Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C++
Article

KeyBoard Hooks

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

Introduction

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)) 
        {
            f1=fopen("c:\\report.txt","a+");
            if (wParam==VK_RETURN)
            {
                ch='\n';
                fwrite(&ch,1,1,f1);
            }
            else
            {
                   BYTE ks[256];
                GetKeyboardState(ks);

                WORD w;
                UINT scan=0;
                ToAscii(wParam,scan,ks,&w,0);
                ch = char(w); 
                fwrite(&ch,1,1,f1);
            }
        fclose(f1);
        }
    }

    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()
{
    f1=fopen("c:\\report.txt","w");
    fclose(f1);
    hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,0);

    return TRUE;
}

License

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

 
QuestionKeyboard Hook working partly Pin
Jayapal Chandran17-Jun-07 1:39
Jayapal Chandran17-Jun-07 1:39 
GeneralRe: Keyboard Hook working partly Pin
foxx 133725-Jul-07 8:02
foxx 133725-Jul-07 8:02 
AnswerRe: Keyboard Hook working partly Pin
Member 132914328-Aug-17 19:45
Member 132914328-Aug-17 19:45 
GeneralThe low level approach Pin
Abdellatif_El_Khlifi5-May-07 12:28
Abdellatif_El_Khlifi5-May-07 12:28 
QuestionProblem after lock Workstation need Help Pin
skywalker327-Mar-07 22:34
skywalker327-Mar-07 22:34 
GeneralWindow movement restriction Pin
AKG20-Feb-07 22:00
AKG20-Feb-07 22:00 
GeneralRe: Window movement restriction Pin
bitslayer14-Mar-07 10:05
bitslayer14-Mar-07 10:05 
Generalhelp Pin
mailtochandra2000@yahoo.com20-Feb-07 18:54
mailtochandra2000@yahoo.com20-Feb-07 18:54 
Generalhelp me Pin
mailtochandra2000@yahoo.com14-Feb-07 23:04
mailtochandra2000@yahoo.com14-Feb-07 23:04 
GeneralRe: help me Pin
DarkWeaver545517-Feb-07 6:42
DarkWeaver545517-Feb-07 6:42 
GeneralKey board hook in dialog based application Pin
mailtochandra2000@yahoo.com14-Feb-07 17:31
mailtochandra2000@yahoo.com14-Feb-07 17:31 
GeneralExcellent Pin
jrivero19-Jan-07 9:59
jrivero19-Jan-07 9:59 
GeneralSimple, but not go be ignored. Pin
Rajesh R Subramanian9-Jan-07 20:02
professionalRajesh R Subramanian9-Jan-07 20:02 
QuestionLicense? Pin
maxinuruguay4-Dec-06 14:01
maxinuruguay4-Dec-06 14:01 
GeneralKeyboard Reference Pin
tuantrang17-Nov-06 16:45
tuantrang17-Nov-06 16:45 
GeneralTranfer Keyboard event to another application Pin
khoipm7-Nov-06 15:55
khoipm7-Nov-06 15:55 
GeneralRe: Tranfer Keyboard event to another application Pin
Young E9-Nov-06 15:47
Young E9-Nov-06 15:47 
GeneralRe: Tranfer Keyboard event to another application Pin
Ngoc Nam31-Dec-06 18:16
Ngoc Nam31-Dec-06 18:16 
GeneralRe: Tranfer Keyboard event to another application Pin
AKG17-Jan-07 2:37
AKG17-Jan-07 2:37 
GeneralRe: Tranfer Keyboard event to another application Pin
Zhi Chen16-Mar-07 18:40
Zhi Chen16-Mar-07 18:40 
GeneralRe: Tranfer Keyboard event to another application Pin
AKG25-Mar-07 23:18
AKG25-Mar-07 23:18 
GeneralRe: Tranfer Keyboard event to another application Pin
pratikmota4-Aug-10 2:52
pratikmota4-Aug-10 2:52 
QuestionHow to hook keyboard events from other processes ? Pin
udiraz10-Oct-06 22:20
udiraz10-Oct-06 22:20 
AnswerRe: How to hook keyboard events from other processes ? Pin
Magnavexx2-Aug-07 15:14
Magnavexx2-Aug-07 15:14 
GeneralWindows hook on Vista Pin
Vivekanand H18-Jul-06 0:23
Vivekanand H18-Jul-06 0:23 

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.