2

Is it possible to send keys to a program without SendMessage and PostMessage API?

kobik
  • 21,001
  • 4
  • 61
  • 121
paulohr
  • 576
  • 1
  • 9
  • 24
  • There are surely methods that don't involve you using those specific apis, but in the end, the keys will still be sent using the message queues. Why not use them directly? – Cray Apr 11 '12 at 13:21
  • Since it's so easy to just send/post a WM_CHAR message, I have never tried anything else. – Martin James Apr 11 '12 at 13:23

1 Answers1

17

The official way to fake input does not involve sending or posting Windows messages directly. Instead you are meant to call SendInput.

When you use SendInput it is indistinguishable from actually pressing the real keys. When you call SendInput to fake keyboard input, the system ultimately posts messages to the message queue of the foreground thread that created the window with the keyboard focus.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • "indistinguishable" : many poker clients that have advanced bot protection tend to disagree :). But generally, a normal program won't tell any difference. – Cray Apr 11 '12 at 14:12
  • @Cray how does poker client notice a difference? – Alex May 05 '12 at 13:37
  • Last I was researching this (say a couple of years ago), there was a lot of talk about GetAsyncKeyState, and how SendInput can't trick that function. But this was obviously one of the easiest methods of checking (it's even a documented ring3 function, nothing sneaky about it), and the hat goes much deeper than that. – Cray May 05 '12 at 17:38
  • @Cray are you aware of any other method that's "indistinguishable" enough? I'm well aware that self-made keyboard drivers should solve this problem, but I'm not familiar with C++.. only C# – Alex May 05 '12 at 17:42
  • 1
    That surely depends on your application, if really are after poker clients, or after something else. If you could do a simple SendInput test and see if your target program cares to even see through that. But otherwise methods to research that I know of are the API functions interception (intercept GetAsyncKeyState, for instance) or the driver you mentioned. You will need knowledge in C for both though. But I am no expert, there may be other ways, good luck! – Cray May 05 '12 at 17:53