Click here to Skip to main content
Click here to Skip to main content

KeyBoard Hooks

By , 23 Jul 2001
Rate this:
Please Sign up or sign in to vote.
<!-- Download Links --> <!-- Main HTML starts here -->


Hooks are one of the most powerful features of Windows. We can hooks to trp all the events in the Windows environment. This example shows how to trap keyboard events and save the keys to a text file.

In the Microsoft® Windows™ operating system, a hook is a mechanism by which a function can intercept events (messages, mouse actions, keystrokes) before they reach an application. The function can act on events and, in some cases, modify or discard them. Functions that receive events are called filter functions and are classified according to the type of event they intercept. For example, a filter function might want to receive all keyboard or mouse events. For Windows to call a filter function, the filter function must be installed — that is, attached to a Windows hook (for example, to a keyboard hook). Attaching one or more filter functions to a hook is known as setting a hook. If a hook has more than one filter function attached, Windows maintains a chain of filter functions. The most recently installed function is at the beginning of the chain, and the least recently installed function is at the end.

When a hook has one or more filter functions attached and an event occurs that triggers the hook, Windows calls the first filter function in the filter function chain. This action is known as calling the hook. For example, if a filter function is attached to the Computer Based Training (CBT) hook and an event that triggers the hook occurs (for example, a window is about to be created), Windows calls the CBT hook by calling the first function in the filter function chain.

To maintain and access filter functions, applications use the SetWindowsHookEx and the UnhookWindowsHookEx functions.

An Example

The CALLBACK function in my example is given below..

LRESULT __declspec(dllexport)__stdcall  CALLBACK KeyboardProc(int nCode,WPARAM wParam, 
                            LPARAM lParam)
    char ch;            
    if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==nCode))
        if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100)) 
            if (wParam==VK_RETURN)
                   BYTE ks[256];

                WORD w;
                UINT scan=0;
                ch = char(w); 

    LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );
    return  RetVal;

The installhook function that is installing the hook function in my example is given below.

BOOL __declspec(dllexport)__stdcall installhook()

    return TRUE;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

H. Joseph

United States United States
No Biography provided

Comments and Discussions

Questionwhat is the first condition for? PinmemberJayapal Chandran3-Aug-13 5:18 
QuestionHow to set Key combination (like SHIFT+F9 etc.) as hook PinmemberMember 783682923-Aug-11 19:57 
QuestionExample in Assembly Language PinmemberMember 810561222-Jul-11 9:38 
GeneralThanks very much Pinmemberachui198031-Mar-11 16:45 
GeneralHook not working fine if Keyboard Language is Change Pinmembereg_Anubhava1-Feb-11 1:30 
Hello experts
I need to use Hindi language in .net, I m added Unicode font through control panel> Regional Language > Language and Install, but in language bar there is only two Hindi key bore one is "Hindi" traditional and other is "devnagri INSCRIPT".
Then i want to monitor Keyboard it is not working fine it shows same result as US Keyboard Language.
If you can think then I Can.

Generalapplication crash when click on iexplore 7 Pinmembercjsc20-Mar-10 10:08 
AnswerRe: application crash when click on iexplore 7 PinmemberBMaguire24-May-11 8:02 
GeneralWhich hook is good for trapping Windows Key + some other Key as shortcut PinmemberArif Saiyed27-Jan-10 2:29 
QuestionDiscard Event PinmemberErin McCarty9-Jun-09 15:51 
QuestionMessageBox within Hook-Function Pinmembernoxmortis12-Aug-08 1:17 
QuestionUnhandled Exception? PinmemberAdrian Parker15-Feb-08 7:56 
AnswerRe: Unhandled Exception? PinmemberAdrian Parker15-Feb-08 8:11 
Questiontyping assistance (hook) Pinmembernguyenkim25-Oct-07 17:09 
Questionare the dll global variables local to each process? Pinmemberfoxx 133725-Jul-07 7:59 
AnswerRe: are the dll global variables local to each process? PinmemberSunny127023-Feb-11 6:35 
QuestionKeyboard Hook working partly PinmemberJayapal Chandran17-Jun-07 1:39 
GeneralRe: Keyboard Hook working partly Pinmemberfoxx 133725-Jul-07 8:02 
GeneralThe low level approach PinmemberEl_Khlifi_Abdellatif5-May-07 12:28 
QuestionProblem after lock Workstation need Help Pinmemberskywalker327-Mar-07 22:34 
GeneralWindow movement restriction PinmemberAKG20-Feb-07 22:00 
GeneralRe: Window movement restriction Pinmemberbitslayer14-Mar-07 10:05 
Generalhelp Pinmembermailtochandra2000@yahoo.com20-Feb-07 18:54 
Generalhelp me Pinmembermailtochandra2000@yahoo.com14-Feb-07 23:04 
GeneralRe: help me PinmemberDarkWeaver545517-Feb-07 6:42 
GeneralKey board hook in dialog based application Pinmembermailtochandra2000@yahoo.com14-Feb-07 17:31 
GeneralExcellent Pinmemberjrivero19-Jan-07 9:59 
GeneralSimple, but not go be ignored. Pinmemberbrahmma9-Jan-07 20:02 
QuestionLicense? Pinmembermaxinuruguay4-Dec-06 14:01 
GeneralKeyboard Reference Pinmembertuantrang17-Nov-06 16:45 
GeneralTranfer Keyboard event to another application Pinmemberkhoipm7-Nov-06 15:55 
GeneralRe: Tranfer Keyboard event to another application PinmemberYoung E9-Nov-06 15:47 
GeneralRe: Tranfer Keyboard event to another application Pinmemberthietlalung31-Dec-06 18:16 
GeneralRe: Tranfer Keyboard event to another application PinmemberAKG17-Jan-07 2:37 
GeneralRe: Tranfer Keyboard event to another application Pinmemberzhi-chen16-Mar-07 18:40 
GeneralRe: Tranfer Keyboard event to another application PinmemberAKG25-Mar-07 23:18 
GeneralRe: Tranfer Keyboard event to another application Pinmemberpratikmota4-Aug-10 2:52 
QuestionHow to hook keyboard events from other processes ? Pinmemberudiraz10-Oct-06 22:20 
AnswerRe: How to hook keyboard events from other processes ? PinmemberMagnavexx2-Aug-07 15:14 
GeneralWindows hook on Vista PinmemberVivekanand H18-Jul-06 0:23 
GeneralRe: Windows hook on Vista Pinmemberskaha_software7-Nov-06 11:17 
GeneralRe: Windows hook on Vista PinmemberJohn McNamara30-Mar-07 5:08 
GeneralRe: Windows hook on Vista Pinmembermywish1-Apr-10 21:34 
AnswerRe: Windows hook on Vista PinmemberAndrianG19-Nov-07 11:15 
QuestionUnHook PinmemberJoe John17-Jul-06 18:19 
AnswerRe: UnHook Pinmemberchick8017-Aug-06 18:43 
GeneralCase Sensitivity and other problems PinmemberGrey|Pixels5-Jul-06 2:28 
GeneralChanging directory of report.txt PinmemberVolarz31-May-06 14:35 
Generaldoesn't work in terminal session Pinmemberbenm9999999924-May-06 16:31 
Generalblocking VK_ENTER using keyboard_hook Pinmemberori kovacsi15-Apr-06 21:14 
QuestionHide Dialog PinmemberJ51219822-Mar-06 18:43 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 24 Jul 2001
Article Copyright 2001 by H. Joseph
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid