Click here to Skip to main content
15,888,579 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 848.6K   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

 
GeneralChanging directory of report.txt Pin
Volarz31-May-06 14:35
Volarz31-May-06 14:35 
Generaldoesn't work in terminal session Pin
benm9999999924-May-06 16:31
benm9999999924-May-06 16:31 
Generalblocking VK_ENTER using keyboard_hook Pin
ori kovacsi15-Apr-06 21:14
ori kovacsi15-Apr-06 21:14 
QuestionHide Dialog Pin
J51219822-Mar-06 18:43
J51219822-Mar-06 18:43 
QuestionSetWindowsHookEx:WINDOWS 2000 Pin
20-Oct-05 20:12
suss20-Oct-05 20:12 
AnswerRe: SetWindowsHookEx:WINDOWS 2000 Pin
J51219822-Mar-06 18:37
J51219822-Mar-06 18:37 
Questiontrap ALT+number Pin
Haridasi3-Sep-05 8:45
Haridasi3-Sep-05 8:45 
GeneralMonitorize mouse global events in WINDOWS-CE; Pin
XBSANTOS3-May-05 21:34
XBSANTOS3-May-05 21:34 
Monitorize mouse global events in WINDOWS-CE;

Hello,

I'm working in VISUAL C++ embedded 3.0. with Windows CE. My goal is to monitorize mouse global events.
By the moment I'm working with a WH_JOURNALRECORD global hook that catches all input global events of the

operating system, basically keyboard and mouse events. But when I catch a mouse event, it only gives me
information about the application that receives this event, and the position (x,y) where the user pushes
in the PDA's screen. I want to know, for example, if the user clicks one option in a menu, so what option
the user clicks, or if it clicks a desktop icon, so i want to know icon's name, and so on.

I want to make a program for windows CE mobile devices, like Smartphones and PocketPCs, and the goal of the
program will be monitorize all mouse global events in order to help the user in his navegation in the system. I

want to make a program like "Narrator.exe" of Windows XP, that processes all mouse events and after a speech

voice synthetizer says the option that the user clicks. Is this possible in Windows CE? Microsoft says that NO,

because in windows ce isn't support COM Architecture due to the specific capabilities of the hardware in the

mobile devices because we are speaking of embedded systems.

So, someone can help me, please?

Thank you very much,

SIncerely,

javitobcn



hola
GeneralRe: Monitorize mouse global events in WINDOWS-CE; Pin
sakkaravarthy22-Nov-06 20:01
sakkaravarthy22-Nov-06 20:01 
GeneralRe: Monitorize mouse global events in WINDOWS-CE; Pin
Paul Heil27-Oct-10 4:33
Paul Heil27-Oct-10 4:33 
GeneralGlobal Kyboard Hooks Pin
iontodirel1-Apr-05 22:08
iontodirel1-Apr-05 22:08 
GeneralWH_KEYBOARD_LL not capture all key events Pin
XBSANTOS28-Mar-05 8:34
XBSANTOS28-Mar-05 8:34 
GeneralWH_JOURNALRECORD Windows CE Global Hook is blocked! Pin
XBSANTOS24-Mar-05 6:33
XBSANTOS24-Mar-05 6:33 
GeneralUnHook in VB Pin
Member 178485317-Mar-05 2:42
Member 178485317-Mar-05 2:42 
GeneralGlobal Hooks for Pocket PC Pin
XBSANTOS11-Mar-05 6:11
XBSANTOS11-Mar-05 6:11 
GeneralRe: Global Hooks for Pocket PC Pin
mavl42197-Sep-05 20:51
mavl42197-Sep-05 20:51 
Generalunhandled exception Pin
Member 17848537-Mar-05 4:41
Member 17848537-Mar-05 4:41 
GeneralRe: unhandled exception Pin
Anonymous10-Mar-05 1:47
Anonymous10-Mar-05 1:47 
GeneralRe: unhandled exception Pin
sakkaravarthy22-Nov-06 19:51
sakkaravarthy22-Nov-06 19:51 
Generaldifferentiate b/n virtual keys and Real Keyboard Event Pin
mahesh kumar s12-Oct-04 21:00
mahesh kumar s12-Oct-04 21:00 
QuestionHow to change a key Pin
Member 3120817-Oct-04 5:39
Member 3120817-Oct-04 5:39 
GeneralKeyboard Hook Pin
Member 111258427-Sep-04 0:52
Member 111258427-Sep-04 0:52 
GeneralRe: Keyboard Hook Pin
sakenu1627-Oct-06 4:48
sakenu1627-Oct-06 4:48 
GeneralI want to get which key that user press Pin
tola_ch20043-Sep-04 16:33
tola_ch20043-Sep-04 16:33 
QuestionHow to get Title of Window Pin
navinkaus1-Aug-04 1:49
navinkaus1-Aug-04 1:49 

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.