Click here to Skip to main content
13,260,161 members (43,242 online)
Click here to Skip to main content
Add your own
alternative version


159 bookmarked
Posted 23 Jul 2001

KeyBoard Hooks

, 23 Jul 2001
Rate this:
Please Sign up or sign in to vote.
This example shows how to write global hooks .This program captures all the Keyboard events and save the keys to a text file.
<!-- 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

You may also be interested in...

Comments and Discussions

GeneralHook not working fine if Keyboard Language is Change Pin
eg_Anubhava1-Feb-11 2:30
membereg_Anubhava1-Feb-11 2:30 
Generalapplication crash when click on iexplore 7 Pin
cjsc20-Mar-10 11:08
membercjsc20-Mar-10 11:08 
AnswerRe: application crash when click on iexplore 7 Pin
BMaguire24-May-11 9:02
memberBMaguire24-May-11 9:02 
GeneralWhich hook is good for trapping Windows Key + some other Key as shortcut Pin
Arif Saiyed27-Jan-10 3:29
memberArif Saiyed27-Jan-10 3:29 
QuestionDiscard Event Pin
Erin McCarty9-Jun-09 16:51
memberErin McCarty9-Jun-09 16:51 
QuestionMessageBox within Hook-Function Pin
noxmortis12-Aug-08 2:17
membernoxmortis12-Aug-08 2:17 
GeneralPlease help me Pin
somnuc8-Aug-08 18:05
membersomnuc8-Aug-08 18:05 
QuestionUnhandled Exception? Pin
Adrian Parker15-Feb-08 8:56
memberAdrian Parker15-Feb-08 8:56 
AnswerRe: Unhandled Exception? Pin
Adrian Parker15-Feb-08 9:11
memberAdrian Parker15-Feb-08 9:11 
Questiontyping assistance (hook) Pin
nguyenkim25-Oct-07 18:09
membernguyenkim25-Oct-07 18:09 
Questionare the dll global variables local to each process? Pin
foxx 133725-Jul-07 8:59
memberfoxx 133725-Jul-07 8:59 
AnswerRe: are the dll global variables local to each process? Pin
Sunny127023-Feb-11 7:35
memberSunny127023-Feb-11 7:35 
QuestionKeyboard Hook working partly Pin
Jayapal Chandran17-Jun-07 2:39
memberJayapal Chandran17-Jun-07 2:39 
GeneralRe: Keyboard Hook working partly Pin
foxx 133725-Jul-07 9:02
memberfoxx 133725-Jul-07 9:02 
AnswerRe: Keyboard Hook working partly Pin
Member 132914328-Aug-17 20:45
memberMember 132914328-Aug-17 20:45 
GeneralThe low level approach Pin
El_Khlifi_Abdellatif5-May-07 13:28
memberEl_Khlifi_Abdellatif5-May-07 13:28 
QuestionProblem after lock Workstation need Help Pin
skywalker327-Mar-07 23:34
memberskywalker327-Mar-07 23:34 
GeneralWindow movement restriction Pin
AKG20-Feb-07 23:00
memberAKG20-Feb-07 23:00 
GeneralRe: Window movement restriction Pin
bitslayer14-Mar-07 11:05
memberbitslayer14-Mar-07 11:05 
Generalhelp Pin
mailtochandra2000@yahoo.com20-Feb-07 19:54
membermailtochandra2000@yahoo.com20-Feb-07 19:54 
Generalhelp me Pin
mailtochandra2000@yahoo.com15-Feb-07 0:04
membermailtochandra2000@yahoo.com15-Feb-07 0:04 
GeneralRe: help me Pin
DarkWeaver545517-Feb-07 7:42
memberDarkWeaver545517-Feb-07 7:42 
GeneralKey board hook in dialog based application Pin
mailtochandra2000@yahoo.com14-Feb-07 18:31
membermailtochandra2000@yahoo.com14-Feb-07 18:31 
GeneralExcellent Pin
jrivero19-Jan-07 10:59
memberjrivero19-Jan-07 10:59 
GeneralSimple, but not go be ignored. Pin
brahmma9-Jan-07 21:02
memberbrahmma9-Jan-07 21:02 
QuestionLicense? Pin
maxinuruguay4-Dec-06 15:01
membermaxinuruguay4-Dec-06 15:01 
GeneralKeyboard Reference Pin
tuantrang17-Nov-06 17:45
membertuantrang17-Nov-06 17:45 
GeneralTranfer Keyboard event to another application Pin
khoipm7-Nov-06 16:55
memberkhoipm7-Nov-06 16:55 
GeneralRe: Tranfer Keyboard event to another application Pin
Young E9-Nov-06 16:47
memberYoung E9-Nov-06 16:47 
GeneralRe: Tranfer Keyboard event to another application Pin
thietlalung31-Dec-06 19:16
memberthietlalung31-Dec-06 19:16 
GeneralRe: Tranfer Keyboard event to another application Pin
AKG17-Jan-07 3:37
memberAKG17-Jan-07 3:37 
GeneralRe: Tranfer Keyboard event to another application Pin
zhi-chen16-Mar-07 19:40
memberzhi-chen16-Mar-07 19:40 
GeneralRe: Tranfer Keyboard event to another application Pin
AKG26-Mar-07 0:18
memberAKG26-Mar-07 0:18 
GeneralRe: Tranfer Keyboard event to another application Pin
pratikmota4-Aug-10 3:52
memberpratikmota4-Aug-10 3:52 
QuestionHow to hook keyboard events from other processes ? Pin
udiraz10-Oct-06 23:20
memberudiraz10-Oct-06 23:20 
AnswerRe: How to hook keyboard events from other processes ? Pin
Magnavexx2-Aug-07 16:14
memberMagnavexx2-Aug-07 16:14 
GeneralWindows hook on Vista Pin
Vivekanand H18-Jul-06 1:23
memberVivekanand H18-Jul-06 1:23 
GeneralRe: Windows hook on Vista Pin
skaha_software7-Nov-06 12:17
memberskaha_software7-Nov-06 12:17 
GeneralRe: Windows hook on Vista Pin
John McNamara30-Mar-07 6:08
memberJohn McNamara30-Mar-07 6:08 
GeneralRe: Windows hook on Vista Pin
mywish1-Apr-10 22:34
membermywish1-Apr-10 22:34 
AnswerRe: Windows hook on Vista Pin
AndrianG19-Nov-07 12:15
memberAndrianG19-Nov-07 12:15 
QuestionUnHook Pin
Joe John17-Jul-06 19:19
memberJoe John17-Jul-06 19:19 
AnswerRe: UnHook Pin
chick8017-Aug-06 19:43
memberchick8017-Aug-06 19:43 
GeneralCase Sensitivity and other problems Pin
Grey|Pixels5-Jul-06 3:28
memberGrey|Pixels5-Jul-06 3:28 
GeneralChanging directory of report.txt Pin
Volarz31-May-06 15:35
memberVolarz31-May-06 15:35 
Generaldoesn't work in terminal session Pin
benm9999999924-May-06 17:31
memberbenm9999999924-May-06 17:31 
Generalblocking VK_ENTER using keyboard_hook Pin
ori kovacsi15-Apr-06 22:14
memberori kovacsi15-Apr-06 22:14 
QuestionHide Dialog Pin
J51219822-Mar-06 19:43
memberJ51219822-Mar-06 19:43 
QuestionSetWindowsHookEx:WINDOWS 2000 Pin
biman ghosh20-Oct-05 21:12
memberbiman ghosh20-Oct-05 21:12 
AnswerRe: SetWindowsHookEx:WINDOWS 2000 Pin
J51219822-Mar-06 19:37
memberJ51219822-Mar-06 19:37 

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 | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 24 Jul 2001
Article Copyright 2001 by H. Joseph
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid