I tried to pop up the menu using Windows API
: SendInput()
, but totally failed every time.
#include <windows.h>
int main (void)
{
INPUT Alt_Space[4] = {0};
Alt_Space[0].type = INPUT_KEYBOARD; // Alt Key down
Alt_Space[0].ki.wVk = VK_MENU;
Alt_Space[1].type = INPUT_KEYBOARD; // Space Key down
Alt_Space[1].ki.wVk = VK_SPACE;
Alt_Space[2].type = INPUT_KEYBOARD; // Space Key up
Alt_Space[2].ki.dwFlags = KEYEVENTF_KEYUP;
Alt_Space[2].ki.wVk = VK_SPACE;
Alt_Space[3].type = INPUT_KEYBOARD; // Alt Key up
Alt_Space[3].ki.dwFlags = KEYEVENTF_KEYUP;
Alt_Space[3].ki.wVk = VK_MENU;
SendInput ( 4, Alt_Space, sizeof(INPUT));
system ("pause");
}
Can anyone know why this code failed to pop up the menu? Thanks.
Compile Command:
mingw32-gcc.exe -O3 -c "Console.c" -o "Console.o"
mingw32-gcc.exe -o "Console.exe" "Console.o"
Though I didn't use SendInput()
, keybd_event()
works perfectly with GCC/Mingw32.
I answered my own question, and leave it in order to provide some idea to whom have the same problem.
// Event
#define KEYEVENTF_KEYDOWN 0x0000
// ALT + SPACE
#define SCAN_CODE_ALT 0x38
#define SCAN_CODE_SPACE 0x39
keybd_event ( VK_MENU , SCAN_CODE_ALT , KEYEVENTF_KEYDOWN, 0);
keybd_event ( VK_SPACE , SCAN_CODE_SPACE, KEYEVENTF_KEYDOWN, 0);
keybd_event ( VK_SPACE , SCAN_CODE_SPACE, KEYEVENTF_KEYUP , 0);
keybd_event ( VK_MENU , SCAN_CODE_ALT , KEYEVENTF_KEYUP , 0);
// UP * 2
#define SCAN_CODE_UP 0x48
keybd_event ( VK_UP , SCAN_CODE_UP , KEYEVENTF_KEYDOWN, 0);
keybd_event ( VK_UP , SCAN_CODE_UP , KEYEVENTF_KEYUP , 0);
keybd_event ( VK_UP , SCAN_CODE_UP , KEYEVENTF_KEYDOWN, 0);
keybd_event ( VK_UP , SCAN_CODE_UP , KEYEVENTF_KEYUP , 0);
// Enter
#define SCAN_CODE_ENTER 0x1C
keybd_event ( VK_RETURN, SCAN_CODE_ENTER, KEYEVENTF_KEYUP , 0);
keybd_event ( VK_RETURN, SCAN_CODE_ENTER, KEYEVENTF_KEYDOWN, 0);