Banananut Banananut - 1 year ago 95
C++ Question

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 ↵).

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download