Question

What I'm looking for is functionality similar to Google desktop. When you hit Ctrl twice rapidly, a little search box pops up.

I've tried using a fairly heavy keyboard hook class that I found somewhere, but that kept giving meunpredictable results (ie first time it would work, then I'd press Ctrl once, and it would raise the key pressed event several times?).

Anyway, this just doesn't seem like it should be complicated, so I feel like I must be doing something wrong. My app (a WPF app) will be running with the primary window's visibility set to hidden, and not residing in the task bar. When I perform the Ctrl - Ctrl, I want to catch it and show the window.

One other side issue - I'm currently doing this by registering a hot key, but that doesn't give me the full functionality I want (the double-press of Ctrl). When the window is shown, I haven't found a 100% fool-proof way of detecting when it no longer has focus (I want to hide it again when this happens). I've tried responding to Deactivated, LostFocus, etc... and those work most of the time. But sometimes it appears that the window is shown without actually being Activated, or receiving focus. Anyy ideas on this as well?

Thanks!
Adam

Was it helpful?

Solution

You'll probably want to use SetWindowsHookEx to hook into the keyboard events at a low level.

Here is some sample code in C++, and here is pinvoke.net's entry, which should help port this to C#.

For C# code, directly usable, you could try this CodeProject sample. From my memory, it worked in most cases, with a few bugs. At one point, I assisted in porting this to the Jad Engine, where we implemented a class (UserActivityHook) that accomplished this, and fixed a few bugs in the process. It might be a very good place to start...

OTHER TIPS

As for the second question, if the form isn't activated when shown, you should call Form.Activate after you display it. For simple debugging, put trace statements in the GotFocus, LostFocus, Enter, Exit, and Activate, Deactivate events and see if any of these meet your needs consistently.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top