In order to send Keystroke to any application window, without activating the application to get input focus. We must get the windows handler first. This requires Windows API FindWindow
and FindWindowsEx
. First, the handle of Top Level window of application is obtained by FindWindow
. Then use FindWindowsEx
to get the handle of the child window or control to receive keys.
Because the top window of the application is not always the window that accepts Keystroke (such as notepad.exe, the window that actually accepts Keystroke is the Edit control under the main window of Notepad), it can be found by ClassID or Caption.
Assuming that the handle of the target window has been got(hwnd), the key message will be sent to the window with PostMessage
.
For normal character keys, it is simplest to use WM_CHAR message directly, as follows:
PostMessage(hwnd, WM_CHAR, 'a', 0);
For un-normal character keys, such as function keys, direction keys, etc., WM_KEYDOWN
and WM_KEYUP
messages should be used as follows:
VirtualKey = MapVirtualKeyA(VK_RIGHT, 0);
PostMessage(hwnd, WM_KEYDOWN, VK_RIGHT, 0x0001|VirtualKey>>16);
PostMessage(hwnd, WM_KEYUP, VK_RIGHT, 0x0001|VirtualKey>>16|0xC0>>24);
The details for last parameter (lParam
) you can reference to msdn.
For the keys "Shift/Ctrl", sample:
keybd_event(VK_SHIFT, 0, 0, 0);
PostMessage(hwnd, WM_KEYDOWN, 0x41, 0x001E0001);
PostMessage(hwnd, WM_KEYUP, 0x41, 0xC01E0001);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
For the keys "Alt", It belongs to the system button, using WM_SYSKEYDOWN
/WM_SYSKEYUP
message. sample:
PostMessage(hwnd, WM_SYSKEYDOWN, VK_F4, 0x003E0001 |0x20000000);
PostMessage(hwnd, WM_SYSKEYUP, VK_F4, 0xC03E0001 | 0x20000000);
0x20000000
means context code, the value is 1 if the "Alt" key is down.