views:

100

answers:

1

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?

+3  A: 

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...

Dean Harding
He can use [BeginInvoke](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.begininvoke.aspx) to get the moral equivalent of `PostMessage` without all that tedious mucking about with P/Invoke.
Logan Capaldo
Can you guys describe in a little more detail what exactly I would be invoking? I would use BeginInvoke to call what?
Siracuse
You would do `window.BeginInvoke(DoStuff);` where DoStuff is the method that does that SendKeys stuff.
Martinho Fernandes
Yes, you're right! I forgot about BeginInvoke :) +1
Dean Harding
Wow, I just tried this and on first testing it works. How is it that this works? By calling the the SendKeys stuff asynchronously I'm ensuring that the KeyUp event has finished?
Siracuse
@SiracuseYou'd use BeginInvoke to invoke a delegate that would do what you're doing in KeyUp now. Calling BeginInvoke from within KeyUp will basically queue up some code to run after KeyUp.
Logan Capaldo
`BeginInvoke` basically does what I said about posting your own custom message: it posts a message to the window's message queue and calls the delegate when that message comes through. It's usually used to ensure background threads update the UI on the UI thread (as they're supposed to) but it works in this situation as well.
Dean Harding