0

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

user4157124
  • 2,809
  • 13
  • 27
  • 42
user1026187
  • 19
  • 1
  • 1
  • 4

2 Answers2

4

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.

Martijn
  • 3,696
  • 2
  • 38
  • 64
  • With AutoIt3, it is almost working but If I send a command directly such as "{LEFT}". It is not handled by the game. I have to repeat the SEND("{LEFT}") ten times in a loop and even in this case sometime the game does not handle the command. – user1026187 Dec 17 '11 at 13:05
  • 4
    What is probably happenning is that the game is polling the {LEFT} signal acynchronously; i.e. it checks whether the key is pressed at a specific moment in time. `Send` simulates a key press that lasts for the tiniest fraction of a second (effectively zero seconds). You probably want to use the DOWN/UP modifier for `Send` (http://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm). – Martijn Dec 17 '11 at 21:42
  • Now, I'm able to control the game programmatically, thank you! :-) – user1026187 Dec 18 '11 at 09:52
  • @Martijn, I've tried your proposed solution but it doesn't work for me. I'm also trying to send input to different applications but neither using autoIT functions, nor using `InputSimulator` doesn't really cut it. Any ideas why ? – Bogdan Molinger Aug 19 '16 at 01:40
1

Any programming language? My recommendation is to write up a little app in C or C++ (although you could also do this in a .NET app with P/Invoke).

Specifically, you're looking for the SendInput function from the Win32 API, which can send low-level keyboard and mouse input to an application. It does this in terms of an INPUT structure that contains the information you want to send.

Of course, use of this function is subject to UIPI, which means that the application you're injecting input into must be running at an equal or lesser integrity level than the application that is doing the injecting.

However, since this function is generally what SendKeys uses under the covers, that last little caveat might be why it isn't working. It's hard to say exactly; you don't tell us what you mean by "do not work".

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574