How can I send keyboard input to an OpenGL / DirectX game window?

How to simulate a key press in a game window (using any programming language on Windows)?

AutoHotKeys Scripts and .NET SendKeys functions not working ...

+1


source to share


3 answers


Using AutoIt! 3 (which is very similar to AutoHotKeys), you should use Send()

( http://www.autoitscript.com/autoit3/docs/functions/Send.htm ), but make sure the game window is activated ( WinActivate()

) before you do this.

I used this to interact with Second Life (which uses OpenGL) successfully. You may require a period Sleep()

between simulated keystrokes, since not all games implement good keyboard buffers.

If that doesn't work, the game will likely access the hardware drivers directly, and your only option is to connect to the keyboard drivers.



If the game runs asynchronously, you want to use flags down

and up

:

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 hold down a key is entirely dependent on how often the program you are trying to control is polling the keyboard; I don't know for sure.

+4


source


Any programming language? My recommendation is to write a small application in C or C ++ (although you can also do this in a .NET application with P / Invoke ).

Specifically, you're looking for a SendInput

function
from the Win32 API that can send low-level keyboard and mouse input to an application. It does this in terms of the structure INPUT

that contains the information you want to send.



Of course, the use of this feature is subject to UIPI, which means that the application you are putting input into must run at an equal or lower integrity level than the application doing the injection.

However, since this feature is typically used SendKeys

behind covers, this last little caveat may be the reason it doesn't work. It's hard to say for sure; you don't tell us what you mean by "don't work".

+1


source


This keyboard wrapper will allow you to simulate keyboard events in games or any other application using C #.

+1


source







All Articles