N. Vendramin
N. Vendramin

Reputation: 1

Keyboard emulation with C#

I am developing a C# application that gives the user the possibility to set some specific keystrokes that the applications will reproduce when it will be launched in execution. As far as I was testing my application in notepads or other simple programs it worked just using SendInput or InputSimulator, but as soon as I tried it with a video-game the emulated input could not be received by the game. The strange thing is that actually the input is received by the game but only if I am using the game chat.

To be clearer I will make an example: I set in my application to reproduce the key w. I launch my application and then I launch a FPS game like CounterStrike. When the applications emulates the pression of the w key my character in the game doesn't move! But if i click on the chat and try to write something in the chatbox the emulated input is recognized and I can see the "w"s being written in the chatbox.

Upvotes: 0

Views: 503

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112299

Game engines usually don't get their input from the usual Windows API; since this one lets you read only one character at once. In games, however, you can press several keys at once. For instance you might press wa together in order to move your character in forward-left direction. Games check the keyboard state at each game loop instead of reading the characters which have been typed. The character “w” might have been typed only once, but the w key might have been in pressed-state during 8 loops. Game engines seem to access the keyboard through a low-level function. This is why commands like SendKeys injecting their keystrokes at a higher level have no effect.

Upvotes: 1

Related Questions