1

I'm trying to program an AI for online games which works by grabbing a screen region of the game area, analyzing the pixels, and then responding with a sequence of keypresses. Basically, it goes like this:

  • Program grabs picture of current frame
  • Program analyzes pixel data and plans accordingly
  • Program sends firefox appropriate keypresses to respond.

I'm currently having problems at step 3 - sending the keypresses. Since I'm building an AI for games, it must send the keypresses realtime. However, I'm noticing with SendKeys.Send()...

  • There's an apparent delay between when I make the request and when firefox executes it
  • It's unpredictable and slow, the keypresses are almost never executed on time
  • Movement is slow and choppy, because there are large gaps in between each processed keypress

Is there any way to predictably send keypress events to a program (such as firefox) realtime? Like when I think about simulating keypresses with a program, I'm thinking of a command that would do the exact same thing as when I press a key on my keyboard, not an unreliable and delayed method to send keypresses to whatever is currently focused. It would be great if anybody had an easy solution to this problem, or maybe any input on what I can do differently? Thanks!

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Superdoggy
  • 218
  • 1
  • 11
  • Try some of the answers here http://stackoverflow.com/questions/5028872/sendkeys-alternative-that-works-on-citrix?rq=1 particularly this http://inputsimulator.codeplex.com/ – Matthew Lock Mar 25 '15 at 01:42
  • Also here http://stackoverflow.com/questions/1138606/alternative-to-sendkeys-when-running-over-remote-desktop – Matthew Lock Mar 25 '15 at 01:44
  • Thanks - is inputsimulator faster, then, I'm assuming? – Superdoggy Mar 25 '15 at 01:49
  • For the other answers - I have looked around and tried quite a few answers though sendkeys was the only one that actually came close to working... – Superdoggy Mar 25 '15 at 01:50
  • inputsimulator uses SendInput rather than SendKeys. I think SendInput is more low level and faster. You might find this helpful in understanding the differences http://www.autohotkey.com/docs/commands/Send.htm "SendInput is generally the preferred method to send keystrokes and mouse clicks because of its superior speed and reliability. Under most conditions, SendInput is nearly instantaneous, even when sending long strings. Since SendInput is so fast, it is also more reliable." – Matthew Lock Mar 25 '15 at 01:51
  • 1
    Thanks! If you wrote this up in an answer (maybe with a code sample...?) then I can upvote it and accept it. :) – Superdoggy Mar 25 '15 at 01:56

1 Answers1

2

The Windows Input Simulator wrapper library uses SendInput rather than SendKeys.

SendInput is generally the preferred method to send keystrokes and mouse clicks because of its superior speed and reliability. Under most conditions, SendInput is nearly instantaneous, even when sending long strings. Since SendInput is so fast, it is also more reliable.

AutoHotKey Send

Matthew Lock
  • 13,144
  • 12
  • 92
  • 130