user4755019
user4755019

Reputation:

Programmaticaly simulating Alt + Enter key press is not working

Here is my code:

keybd_event(VK_MENU, 0, 0, 0);
keybd_event(VK_RETURN, 0, 0, 0);
Sleep(200);
keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);

The first line would press Alt
The second line would press Enter ↵ (or Return ↵),
The fourth line would release Alt,
The fifth line would release Enter ↵ (or Return ↵).

Upvotes: 0

Views: 1717

Answers (1)

NathanOliver
NathanOliver

Reputation: 180825

You are not setting the KEYEVENTF_EXTENDEDKEY flag to keep the keys pressed down. Change your code to:

keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_EXTENDEDKEY, 0);
Sleep(200);
keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);

Also you really don't need the sleep in the middle if you are just sending a Alt + Enter

You can see all of the keycodes here at the MSDN page.

  • Alt = VK_MENU
  • Left Alt = VK_LMENU
  • Right Alt Gr = VK_RMENU

Upvotes: 1

Related Questions