Click here to Skip to main content
14,212,441 members
Click here to Skip to main content
Article
Posted 23 Jul 2001

Stats

747.9K views
18.9K downloads
160 bookmarked

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.

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

Share

About the Author

H. Joseph
United States United States
No Biography provided

Comments and Discussions

 
QuestionHow to dp - when I need Global Keyboard hook that some system key does not work. Pin
Eugene Kim23-Jan-03 11:52
memberEugene Kim23-Jan-03 11:52 
QuestionHow do I detect the keystroke when a different process has the focus Pin
mgrishaber15-Jan-03 10:53
membermgrishaber15-Jan-03 10:53 
AnswerRe: How do I detect the keystroke when a different process has the focus Pin
Alexander Ruscle18-Aug-03 12:59
memberAlexander Ruscle18-Aug-03 12:59 
GeneralRe: How do I detect the keystroke when a different process has the focus Pin
alex.barylski18-Nov-03 20:46
memberalex.barylski18-Nov-03 20:46 
Questioncan i find all active hooks? Pin
BugByter25-Dec-02 5:11
memberBugByter25-Dec-02 5:11 
Generalstore hooked data Pin
18-Dec-02 5:04
suss18-Dec-02 5:04 
GeneralMap a key over another... Pin
cionci5-Dec-02 6:57
membercionci5-Dec-02 6:57 
GeneralRe: Map a key over another... Pin
Daniel Turini5-Dec-02 8:11
memberDaniel Turini5-Dec-02 8:11 
GeneralRe: Map a key over another... Pin
cionci9-Dec-02 8:07
membercionci9-Dec-02 8:07 
GeneralRe: Map a key over another... Pin
cionci10-Dec-02 7:53
membercionci10-Dec-02 7:53 
GeneralRe: Map a key over another... Pin
shawkins9-Feb-04 9:19
membershawkins9-Feb-04 9:19 
QuestionDoes this work under Win98? Pin
Mehdi Mousavi31-Aug-02 3:29
memberMehdi Mousavi31-Aug-02 3:29 
Generalit doesn't work with windows dialog to log on... Pin
Anonymous21-Aug-02 9:13
memberAnonymous21-Aug-02 9:13 
GeneralRe: it doesn't work with windows dialog to log on... Pin
Joan M14-Nov-02 1:44
professionalJoan M14-Nov-02 1:44 
GeneralA question Pin
Anonymous12-Aug-02 3:23
memberAnonymous12-Aug-02 3:23 
QuestionWhy not use MapVirtualKey? Pin
Anonymous16-Jul-02 18:37
memberAnonymous16-Jul-02 18:37 
QuestionCan I use this DLL in Delphi? Pin
cassio12-Jul-02 3:11
membercassio12-Jul-02 3:11 
AnswerRe: Can I use this DLL in Delphi? Pin
Anonymous18-Jul-02 19:56
memberAnonymous18-Jul-02 19:56 
GeneralRemoving Hooked Message Pin
7-Jul-02 8:38
suss7-Jul-02 8:38 
GeneralFinding the app which the key press occured in Pin
8-Jun-02 11:56
suss8-Jun-02 11:56 
GeneralRe: Finding the app which the key press occured in Pin
7-Jul-02 8:35
suss7-Jul-02 8:35 
GeneralToAscii() and the dead keys Pin
Lanawan3-Jun-02 17:38
memberLanawan3-Jun-02 17:38 
GeneralWindows Hook not running under service application Pin
Dung Le25-Apr-02 20:19
memberDung Le25-Apr-02 20:19 
GeneralRe: Windows Hook not running under service application Pin
8-May-02 21:21
suss8-May-02 21:21 
Generalregister it within MS Information tool... Pin
sedonya17-Apr-02 12:59
membersedonya17-Apr-02 12:59 

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.

Layout: fixed | fluid

Article Copyright 2001 by H. Joseph
Everything else Copyright © CodeProject, 1999-2019

Server Web01
Version 2.8.190619.2