Detect Simulated vs Physical key presses?

Aug 1, 2011 at 12:56 AM

I'm working on a macro recorder/player, and during playback I want to block physical user key presses until the macro's simulated input is complete.

My keyboard handler is hooked in to catch all keystrokes, and allows me to watch for, and start simulating key presses at the right time:

static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId);

I can block key strokes, but but since the simulated key presses go through the same handler (ie: "indistinguishable"), I won't know if the user pressed a physical key, or if the key was a simulated key press.

Any ideas?

Thanks for the great library!

-AAron

Coordinator
Aug 7, 2011 at 11:25 PM
Hi Aaron,

I'm not aware that you can detect whether the keypress was from the API call (simulated) or the physical keyboard (physical).

My recollection is that when you make a single call to the SendInput API that entire series of simulated inputs is not-interruptible (ie; the full set of simulated input is completed as one chunk of input).

From the testing I did at the time I could interleave physical input with simulated input if I made lots of calls to SendInput, but I couldn't interrupt a "chunk" of input sent in one call.

Refer to the following where the documentation states: The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream. These events are not interspersed with other keyboard or mouse input events inserted either by the user (with the keyboard or mouse) or by calls tokeybd_event, mouse_event, or other calls to SendInput.

Hope that helps!
Mike

On Mon, Aug 1, 2011 at 10:56 AM, aaronnaas <notifications@codeplex.com> wrote:

From: aaronnaas

I'm working on a macro recorder/player, and during playback I want to block physical user key presses until the macro's simulated input is complete.

My keyboard handler is hooked in to catch all keystrokes, and allows me to watch for, and start simulating key presses at the right time:

static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId);

I can block key strokes, but but since the simulated key presses go through the same handler (ie: "indistinguishable"), I won't know if the user pressed a physical key, or if the key was a simulated key press.

Any ideas?

Thanks for the great library!

-AAron

Read the full discussion online.

To add a post to this discussion, reply to this email (inputsimulator@discussions.codeplex.com)

To start a new discussion for this project, email inputsimulator@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


May 30, 2012 at 8:53 AM

It is possible to distinguish fake key presses from real ones using the
LLKHF_INJECTED flag: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644967%28v=vs.85%29.aspx

You can do something like

flags = (uint)lParam;
bool isKeyInjected = (flags & 0x00000010) > 0;

In practice it might give you "false" positives: for example my Logitech
keyboard sends media key events with the injected flag turned on for
some reason.