Click here to Skip to main content
15,439,556 members
Articles / Programming Languages / C++
Posted 23 Jul 2001


163 bookmarked

KeyBoard Hooks

Rate me:
Please Sign up or sign in to vote.
4.76/5 (39 votes)
23 Jul 20012 min read
This example shows how to write global hooks .This program captures all the Keyboard events and save the keys to a text file.


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

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionVery bad Pin
Jaime Stuardo - Chile16-Mar-22 4:57
MemberJaime Stuardo - Chile16-Mar-22 4:57 
Questionalgorithm Pin
Member 136608224-Feb-18 15:36
MemberMember 136608224-Feb-18 15:36 
QuestionRe: what is HC_ACTION Pin
Member 1304905128-Mar-17 3:36
MemberMember 1304905128-Mar-17 3:36 
QuestionSet DLL Project path. Pin
PrashantBarghare14-Aug-15 7:11
MemberPrashantBarghare14-Aug-15 7:11 
Questionwhat is the first condition for? Pin
Jayapal Chandran3-Aug-13 5:18
MemberJayapal Chandran3-Aug-13 5:18 
AnswerRe: what is the first condition for? Pin
Rajesh R Subramanian23-Oct-14 19:55
professionalRajesh R Subramanian23-Oct-14 19:55 
GeneralRe: what is the first condition for? Pin
Member 1304905128-Mar-17 3:34
MemberMember 1304905128-Mar-17 3:34 
AnswerRe: what is the first condition for? Pin
Rajesh R Subramanian28-Mar-17 13:45
professionalRajesh R Subramanian28-Mar-17 13:45 
GeneralRe: what is the first condition for? Pin
Member 1304905128-Mar-17 23:00
MemberMember 1304905128-Mar-17 23:00 
QuestionHow to set Key combination (like SHIFT+F9 etc.) as hook Pin
Member 783682923-Aug-11 19:57
MemberMember 783682923-Aug-11 19:57 
QuestionExample in Assembly Language Pin
Member 810561222-Jul-11 9:38
MemberMember 810561222-Jul-11 9:38 
GeneralThanks very much Pin
achui198031-Mar-11 16:45
Memberachui198031-Mar-11 16:45 
GeneralHook not working fine if Keyboard Language is Change Pin
Anubhava Dimri1-Feb-11 1:30
MemberAnubhava Dimri1-Feb-11 1:30 
Generalapplication crash when click on iexplore 7 Pin
cjsc20-Mar-10 10:08
Membercjsc20-Mar-10 10:08 
AnswerRe: application crash when click on iexplore 7 Pin
BMaguire24-May-11 8:02
MemberBMaguire24-May-11 8:02 
GeneralWhich hook is good for trapping Windows Key + some other Key as shortcut Pin
Arif Saiyed27-Jan-10 2:29
MemberArif Saiyed27-Jan-10 2:29 
QuestionDiscard Event Pin
Erin McCarty9-Jun-09 15:51
MemberErin McCarty9-Jun-09 15:51 
QuestionMessageBox within Hook-Function Pin
noxmortis12-Aug-08 1:17
Membernoxmortis12-Aug-08 1:17 
GeneralPlease help me Pin
somnuc8-Aug-08 17:05
Membersomnuc8-Aug-08 17:05 
QuestionUnhandled Exception? Pin
Adrian Parker15-Feb-08 7:56
MemberAdrian Parker15-Feb-08 7:56 
AnswerRe: Unhandled Exception? Pin
Adrian Parker15-Feb-08 8:11
MemberAdrian Parker15-Feb-08 8:11 
Questiontyping assistance (hook) Pin
nguyenkim25-Oct-07 17:09
Membernguyenkim25-Oct-07 17:09 
Questionare the dll global variables local to each process? Pin
foxx 133725-Jul-07 7:59
Memberfoxx 133725-Jul-07 7:59 
AnswerRe: are the dll global variables local to each process? Pin
Sunny127023-Feb-11 6:35
MemberSunny127023-Feb-11 6:35 
QuestionKeyboard Hook working partly Pin
Jayapal Chandran17-Jun-07 1:39
MemberJayapal Chandran17-Jun-07 1:39 

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.