I'm using the following project to handle global keyboard and mouse hooking in my C# application.
This project is basically a wrapper around the Win API call SetWindowsHookEx using either the WH_MOUSE_LL or WH_KEYBOARD_LL constants. It also manages certain state and generally makes this kind of hooking pretty pain free.
I'm using this for a mouse gesture recognition software I'm working on. Basically, I have it setup so it detects when a global hotkey is pressed down (say CTRL), then the user moves the mouse in the shape of a pre-defined gesture and then releases global hotkey.
The event for the KeyDown is processed and tells my program to start recording the mouse locations until it receives the KeyUp event. This is working fine and it allows an easy way for users to enter a mouse-gesture mode. Once the KeyUp event fires and it detects the appropriate gesture, it is suppose to send certain keystrokes to the active window that the user has defined for that particular gesture they just drew.
I'm using the SendKeys.Send/SendWait methods to send output to the current window.
My problem is this: When the user releases their global hotkey (say CTRL), it fires the KeyUp event. My program takes its recorded mouse points and detects the relevant gesture and attempts to send the correct input via SendKeys.
However, because all of this is in the KeyUp event, that global hotkey hasn't finished being processed. So, for example if I defined a gesture to send the key "A" when it is detected, and my global hotkey is CTRL, when it is detected SendKeys will send "A" but while CTRL is still "down". So, instead of just sending A, I'm getting CTRL-A. So, in this example, instead of physically sending the single character "A" it is selecting-all via the CTRL-A shortcut. Even though the user has released the CTRL (global hotkey), it is still being considered down by the system.
Once my KeyUp event fires, how can I have my program wait some period of time or for some event so I can be sure that the global hotkey is truly no longer being registered by the system, and only then sending the correct input via SendKeys?
You could simply set up a timer and fire the SendKeys a fraction of a second later (say, 100ms) the user isn't going to notice the slight delay, but it would be enough that it happens outside the event handler for the KeyUp.
Another alternative would be to post your own window (using PostMessage
) a custom message to signal when it's supposed to "trigger" the effect and actually do the SendKeys stuff in that custom message handler. This has the advantage of not using some arbitrary "delay". It's slightly more complicated to set up though (particularly in .NET where you'd have to P/Invoke a few more API calls to get it to work, override the WndProc
function and so on...