Search code examples
c#formseventsonkeydownuser32

Detecting KeyDown and KeyUp events with no form in focus (nor in foreground)


I wish the user to be able to press the Ctrl, and my form does something and upon releasing this key, the form does something else. Unfortunately, my form may not be in focus (nor the foreground window). How can I achieve this? I'm guessing it's a PInvoke, but a search didn't turn up anything obvious.

Thanks Thomas


Solution

  • You need to use SetWindowsHook for this. You application will get a callback when the keyboard is triggered.

    You should checkout this example code: http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx