Skip to main content
Email Password   helpLost your password?
Screenshot - key_preview.jpg

Introduction

This article discusses a class that I wrote that wraps a global low level keyboard hook. The sample hooks the A and B keys, just for demonstration.

Background

I was trying to find a way for an application that I am writing to restore itself when a combination of keys was pressed. This was born from searching around for the answer.

Using the Code

First download the source, and add globalKeyboardHook.cs to your project. Then add...

using Utilities;

... to the top of the file you are going to use it in. Next add an instance of globalKeyboardHook to your class:

globalKeyboardHook gkh = new globalKeyboardHook() ;

When a globalKeyboardHook is constructed, it automatically installs the hook, so all there is left to do is add some keys for it to watch, and define some event handlers for the KeyDown and KeyUp events. I usually do this on the main form's load event handler, like this:

private void Form1_Load(object sender, EventArgs e) {
    gkh.HookedKeys.Add(Keys.A);
    gkh.HookedKeys.Add(Keys.B);
    gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
    gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
} 

void gkh_KeyUp(object sender, KeyEventArgs e) {
    lstLog.Items.Add("Up\t" + e.KeyCode.ToString());
    e.Handled = true ;
}

void gkh_KeyDown(object sender, KeyEventArgs e) {
    lstLog.Items.Add("Down\t" + e.KeyCode.ToString());
    e.Handled = true ;
} 

Here I have chosen to watch for the A and B keys, and defined handlers for the KeyUp and KeyDown events that both log to a listbox called lstLog. So whenever the user presses the A or B keys, no matter what has focus, the application will be notified. Setting e.Handled to true makes it so no other notifications for this event go out, in the sample, this effectively stops the user from typing an A or B. This can be useful in ensuring that key combinations are not also typed out when used.

You can add hooks for as many keys as you would like, just add them like above. Don't get frustrated if you add a hook for a key and it doesn't work, many of them, like Keys.Shift show up as other more specific keys, like Keys.LShiftKey or Keys.RShiftKey. Keys.Alt shows up as Keys.LMenu or Keys.RMenu, Keys.Control shows up as Keys.LControl or Keys.RControl, just to name a few.

If you would like to hook or unhook the keyboard hook at any point, just call your globalKeyboardHook's hook and unhook methods, like so:

//unhook
gkh.unhook() 
//set the hook again
gkh.hook() 

Points of Interest

The bulk of the work in this code is done in the globalKeyboardHook class, although it is a fairly simple piece of code itself. The hardest part of doing this was finding the correct parameters for SetWindowsHookEx.

[DllImport("user32.dll")]
static extern IntPtr SetWindowsHookEx
    (int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId);

The parameters were worked out to be:

IntPtr hInstance = LoadLibrary("User32");
hhook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hInstance, 0);

The first parameter WH_KEYBOARD_LL is just saying that we want to hook the low level keyboard events, hookProc is the callback for the event, hInstance is a handle to User32.dll, where this event is first processed (I think). The last parameter is if you want to hook a specific thread, then you would just pass a thread id instead of using the hInstance.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralCallback Error Pin
Voulnet
6:38 29 Oct '09  
GeneralRe: Callback Error Pin
Voulnet
7:32 29 Oct '09  
GeneralGreat Pin
Ravenet
8:24 9 Oct '09  
GeneralAlt + Q Pin
a.xyz.b.1
8:40 6 Jul '09  
GeneralStops Capturing Keys Pin
Nate Shoffner
13:23 24 May '09  
GeneralRe: Stops Capturing Keys Pin
berni2704
4:27 16 Oct '09  
Questiontransfor it to wpf Pin
sonicred
2:09 2 Apr '09  
AnswerRe: transfor it to wpf Pin
StormySpike
8:55 2 Apr '09  
GeneralRe: transfor it to wpf Pin
sonicred
14:48 3 Apr '09  
GeneralRe: transfor it to wpf Pin
StormySpike
14:47 20 Apr '09  
GeneralRe: transfor it to wpf Pin
panoylhs
5:42 22 Sep '09  
Generalunhook does not work, and when i capture the keyboard you cant type :( Pin
mysquad
6:26 13 Mar '09  
GeneralRe: unhook does not work, and when i capture the keyboard you cant type :( Pin
StormySpike
15:39 13 Mar '09  
GeneralMDA error garbage collected delegate Pin
AzraelKans
14:26 2 Feb '09  
GeneralRe: MDA error garbage collected delegate Pin
StormySpike
15:45 13 Mar '09  
GeneralRe: MDA error garbage collected delegate Pin
weso
16:54 18 Aug '09  
GeneralRe: MDA error garbage collected delegate Pin
Member 2516506
11:31 4 Oct '09  
GeneralRe: MDA error garbage collected delegate Pin
avihaydar10
23:21 12 Oct '09  
GeneralRe: MDA error garbage collected delegate Pin
kendolew
6:43 23 Oct '09  
GeneralHow to use this for capturing all the keys? Pin
Member 4754326
10:46 28 Jan '09  
GeneralRe: How to use this for capturing all the keys? Pin
StormySpike
15:41 13 Mar '09  
GeneralRe: How to use this for capturing all the keys? Pin
jwainer
15:33 6 Aug '09  
GeneralNot capturing after random number of key strokes ! Pin
Member 4754326
21:38 11 Jan '09  
GeneralMy vote of 1 Pin
Member 4072865
9:29 20 Dec '08  
GeneralMy vote of 1 Pin
Member 4072865
8:00 20 Dec '08  


Last Updated 30 May 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009