Win32 simulate held down key

Yossarian22

[H]ard|Gawd
Joined
Apr 27, 2009
Messages
1,799
I had a couple of questions and I wonder if anyone could answer.

I've been toying around with Win32API the past week or so and I'm trying to simulate keyboard input, namely a key held down/typematics.

The best method (besides virtualizing a device with Raw Input/DirectInput) I've found is using SendInput() because:
  1. Sends input to currently focused window/control child (if want other target, see PostMessage() but be careful)
  2. Can send an array of INPUT messages
  3. Can use virtual keys (VK_VOLUME_MUTE) or go right for the scan code
  4. No hooking required because it places the message in Window's global input queue

My main problem is that when you set the key state in dwFlags, it reads the datum and then deques it (Window's global input queue) and moves on to the next message, e.g.:

http://pastebin.com/qdEfY1F5

Sample_Output said:
$a.exe
a *3sec* a *3sec* a
^C
$

Okay, that makes sense. It just isn't as intuitive as it first seems (If the key is in a down state, expect it to perform typematic repeat, right? Guess not.) and it becomes obvious that you're going to have to create a thread for asynchronous operation (handling the message and passing it on to Window's message queue). For efficient operation you're probably going to want the thread listening for messages to NOT run in a polling mode -- but rather interrupt/event driven.

The only other solution I could think of is to push/enqueue the simulated messages (which can be determined by some sentinel in dwExtraInfo) back on the queue until you get a KEYUP message... but you'd have to be careful how you kept track of the state of the key. Not to mention you probably have to be running in ring 0/use a hook since essentially you're changing the rules of the input queue.

Unless I am overthinking this entire thing/user error? :confused:
 
Back
Top