Click here to Skip to main content
14,422,139 members

KeyBoard Hooks

Rate this:
4.76 (39 votes)
Please Sign up or sign in to vote.
4.76 (39 votes)
23 Jul 2001
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


About the Author

H. Joseph
United States United States
No Biography provided

Comments and Discussions

QuestionHow to change a key Pin
Member 3120817-Oct-04 6:39
MemberMember 3120817-Oct-04 6:39 
GeneralKeyboard Hook Pin
Member 111258427-Sep-04 1:52
MemberMember 111258427-Sep-04 1:52 
GeneralRe: Keyboard Hook Pin
sakenu1627-Oct-06 5:48
Membersakenu1627-Oct-06 5:48 
GeneralI want to get which key that user press Pin
tola_ch20043-Sep-04 17:33
Membertola_ch20043-Sep-04 17:33 
QuestionHow to get Title of Window Pin
navinkaus1-Aug-04 2:49
Membernavinkaus1-Aug-04 2:49 
AnswerRe: How to get Title of Window Pin
asdklasjdlaskj6-Feb-07 8:56
Memberasdklasjdlaskj6-Feb-07 8:56 
GeneralI want help Pin
ibikhan17-Jul-04 2:03
Memberibikhan17-Jul-04 2:03 
GeneralRe: I want help Pin
sakkaravarthy22-Nov-06 20:54
Membersakkaravarthy22-Nov-06 20:54 
Generalaccess datawindow Pin
tang1000713-Nov-03 22:02
Membertang1000713-Nov-03 22:02 
GeneralHookApi SourceCode Pin
xiamy8-Nov-03 7:32
Memberxiamy8-Nov-03 7:32 
GeneralRe: HookApi SourceCode Pin
Mehta Jigar12-Dec-05 10:51
MemberMehta Jigar12-Dec-05 10:51 
GeneralWindows HOOKS Pin
Member 59936722-Sep-03 20:08
MemberMember 59936722-Sep-03 20:08 
GeneralRe: Windows HOOKS Pin
disabledman27-Jan-04 21:54
Memberdisabledman27-Jan-04 21:54 
QuestionHow do I Intercept Windows Start-Menu key? Pin
JMP-5520-Aug-03 10:49
MemberJMP-5520-Aug-03 10:49 
AnswerRe: How do I Intercept Windows Start-Menu key? Pin
Tiogshi2-Dec-03 12:45
MemberTiogshi2-Dec-03 12:45 
Generalbut other processe can`t call the function Pin
bruce61119-Aug-03 19:26
Memberbruce61119-Aug-03 19:26 
Generalconsole mode Pin
kafchan200316-Aug-03 21:24
Memberkafchan200316-Aug-03 21:24 
GeneralRe: console mode Pin
nghiath12-Oct-05 5:43
Membernghiath12-Oct-05 5:43 
Generalneed a hook Pin
Mgazza6-Jul-03 11:14
MemberMgazza6-Jul-03 11:14 
GeneralWindows Special keys trapping Pin
fyloo2-Jul-03 15:07
Memberfyloo2-Jul-03 15:07 
Generalgetting the handle of a destroyed window Pin
Anonymous24-Jun-03 0:40
MemberAnonymous24-Jun-03 0:40 
vinhquang24-May-03 8:01
Membervinhquang24-May-03 8:01 
GeneralHooking windows API (like CreateFile) Pin
Fad B2-Mar-03 6:23
MemberFad B2-Mar-03 6:23 
GeneralRe: Hooking windows API (like CreateFile) Pin
Alhimovich Alexander23-Mar-03 12:51
MemberAlhimovich Alexander23-Mar-03 12:51 
GeneralRe: Hooking windows API (like CreateFile) Pin
Anonymous28-Jun-03 0:42
MemberAnonymous28-Jun-03 0:42 

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.

Posted 23 Jul 2001


160 bookmarked