A feature of AHK scripts is that they can send keystrokes to a window, the window interprets the keystokes and displays them accordingly. The function that is used is called ControlSend
in AHK.
I am curious about what this function actually does underneath in the Win32 API. I assumed it would make use of SendMessage
/PostMessage
; however, I tested both of them to send a message to the window using WM_KEYDOWN
, and the window could not interpret it.
It's not simulating the keyboard with keybd_event
or SendInput
either, because it manages to send keystrokes even when the process window is in the background and unfocused.
So, which functions does ControlSend
in AHK actually rely on in the Win32 API?
This is an example AHK script:
Loop{
sendKey("a")
}
F1::
if (mypid == 0){
WinGet, var, PID, A
mypid = %var%
MsgBox, Found client %mypid%
}
sendKey(key)
{
global mypid
WinClose
ControlSend,,{%key%}, ahk_pid %mypid%
}