Click here to Skip to main content
14,135,406 members
Click here to Skip to main content
Add your own
alternative version


163 bookmarked
Posted 30 May 2007
Licenced CPOL

A Simple C# Global Low Level Keyboard Hook

, 30 May 2007
Rate this:
Please Sign up or sign in to vote.
A simple description and sample of creating a global low level keyboard hook in C#
Screenshot - key_preview.jpg


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.


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

//set the hook again

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.

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.


  • 5/30/07 - First version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United States United States
I currently work as a Software Engineer for a company in North Carolina, mainly working with C#.

You may also be interested in...


Comments and Discussions

QuestionRe: CallbackOnCollectedDelegate Pin
LenFH13-Aug-07 11:28
memberLenFH13-Aug-07 11:28 
AnswerRe: CallbackOnCollectedDelegate Pin
dubious_mauvo15-Aug-07 2:07
memberdubious_mauvo15-Aug-07 2:07 
GeneralRe: CallbackOnCollectedDelegate Pin
kirillkornyakov2-Feb-10 22:50
memberkirillkornyakov2-Feb-10 22:50 
GeneralRe: CallbackOnCollectedDelegate Pin
Haitham Khedre30-Mar-10 7:41
memberHaitham Khedre30-Mar-10 7:41 
GeneralRe: CallbackOnCollectedDelegate Pin
jrbosch2-Jan-11 22:52
memberjrbosch2-Jan-11 22:52 
GeneralRe: CallbackOnCollectedDelegate Pin
rafinhacsm27-Dec-11 1:01
memberrafinhacsm27-Dec-11 1:01 
GeneralRe: CallbackOnCollectedDelegate Pin
dullan27-Feb-12 15:46
memberdullan27-Feb-12 15:46 
AnswerRe: CallbackOnCollectedDelegate Pin
jeanlucvalestin16-Nov-08 1:14
memberjeanlucvalestin16-Nov-08 1:14 
GeneralRe: CallbackOnCollectedDelegate Pin
jeanlucvalestin16-Nov-08 1:16
memberjeanlucvalestin16-Nov-08 1:16 
GeneralRe: CallbackOnCollectedDelegate Pin
Ozan Müyesseroğlu29-Dec-11 14:31
memberOzan Müyesseroğlu29-Dec-11 14:31 
GeneralSet hooked key by variable Pin
~Rob9-Jul-07 5:03
member~Rob9-Jul-07 5:03 
AnswerRe: Set hooked key by variable Pin
StormySpike10-Jul-07 16:36
memberStormySpike10-Jul-07 16:36 
GeneralRe: Set hooked key by variable Pin
lancesh3-Apr-08 4:55
memberlancesh3-Apr-08 4:55 
GeneralRe: Set hooked key by variable Pin
StormySpike16-Apr-08 5:00
memberStormySpike16-Apr-08 5:00 
GeneralWindows Vista Pin
none47-Jul-07 2:29
membernone47-Jul-07 2:29 
GeneralRe: Windows Vista Pin
none47-Jul-07 2:32
membernone47-Jul-07 2:32 
GeneralHi from Guayquil-Ecuador Pin
OneSoftware30-May-07 20:27
memberOneSoftware30-May-07 20:27 
AnswerRe: Hi from Guayquil-Ecuador Pin
StormySpike31-May-07 2:39
memberStormySpike31-May-07 2:39 
GeneralThanks, good job and powerfull (keylogger in mind?) Pin
OneSoftware31-May-07 6:16
memberOneSoftware31-May-07 6:16 
AnswerRe: Thanks, good job and powerfull (keylogger in mind?) Pin
StormySpike31-May-07 7:19
memberStormySpike31-May-07 7:19 
GeneralRe: Thanks agains! Pin
OneSoftware31-May-07 18:46
memberOneSoftware31-May-07 18:46 
GeneralRe: Thanks, good job and powerfull (keylogger in mind?) Pin
Andreas Kroll5-Jun-07 0:41
memberAndreas Kroll5-Jun-07 0:41 
GeneralThis website are pretty and powerful (! Pin
OneSoftware5-Jun-07 19:27
memberOneSoftware5-Jun-07 19:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190518.1 | Last Updated 30 May 2007
Article Copyright 2007 by StormySpike
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid