How can I simulate a keypress in a game window (using any programming language in Windows)?
AutoHotKeys Scripts and .NET SendKeys functions do not work...
Using AutoIt!3 (which is quite similar to AutoHotKeys), you'd use Send()
(http://www.autoitscript.com/autoit3/docs/functions/Send.htm), but make sure to have the game window active (WinActivate()
) before you do.
I've used this to interact with Second Life (which uses OpenGL) succesfully. You may require a Sleep()
period between simulated key presses, since not all games implement good keyboard buffers.
If this doesn't work, the game is probably accessing the hardware drivers directly, and your only option is to hook into the keyboard drivers.
If the game is polling asynchonously, you want to use the down
and up
modifiers flags:
Send("{left down}") ; hold down the LEFT key
Sleep(10) ; keep it pressed for 10 milliseconds
Send("{left up}") ; release the LEFT key
Figuring out how long to keep the key pressed depends entirely on how frequently the program you're trying to control is polling the keyboard; no way to know for sure.