Click here to Skip to main content
6,305,776 members and growing! (16,255 online)
Email Password   helpLost your password?
General Programming » DLLs & Assemblies » Hooks     Intermediate

KeyBoard Hooks

By H. Joseph

This example shows how to write global hooks .This program captures all the Keyboard events and save the keys to a text file.
VC6Win2K, Dev
Posted:23 Jul 2001
Views:316,123
Bookmarked:110 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
45 votes for this article.
Popularity: 6.66 Rating: 4.03 out of 5
5 votes, 16.7%
1
1 vote, 3.3%
2
2 votes, 6.7%
3
5 votes, 16.7%
4
17 votes, 56.7%
5

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

About the Author

H. Joseph


Member

Location: United States United States

Other popular DLLs & Assemblies articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 119 (Total in Forum: 119) (Refresh)FirstPrevNext
QuestionDiscard Event PinmemberErin McCarty16:51 9 Jun '09  
QuestionMessageBox within Hook-Function Pinmembernoxmortis2:17 12 Aug '08  
GeneralPlease help me Pinmembersomnuc18:05 8 Aug '08  
GeneralUnhandled Exception? PinmemberAdrian Parker8:56 15 Feb '08  
GeneralRe: Unhandled Exception? PinmemberAdrian Parker9:11 15 Feb '08  
Questiontyping assistance (hook) Pinmembernguyenkim18:09 25 Oct '07  
Questionare the dll global variables local to each process? Pinmemberfoxx 13378:59 25 Jul '07  
QuestionKeyboard Hook working partly PinmemberJayapal Chandran2:39 17 Jun '07  
GeneralRe: Keyboard Hook working partly Pinmemberfoxx 13379:02 25 Jul '07  
GeneralThe low level approach PinmemberEl_Khlifi_Abdellatif13:28 5 May '07  
QuestionProblem after lock Workstation need Help Pinmemberskywalker323:34 27 Mar '07  
GeneralWindow movement restriction PinmemberAKG23:00 20 Feb '07  
GeneralRe: Window movement restriction Pinmemberbitslayer11:05 14 Mar '07  
Generalhelp Pinmembermailtochandra2000@yahoo.com19:54 20 Feb '07  
Generalhelp me Pinmembermailtochandra2000@yahoo.com0:04 15 Feb '07  
GeneralRe: help me PinmemberDarkWeaver54557:42 17 Feb '07  
GeneralKey board hook in dialog based application Pinmembermailtochandra2000@yahoo.com18:31 14 Feb '07  
GeneralExcellent Pinmemberjrivero10:59 19 Jan '07  
GeneralSimple, but not go be ignored. Pinmemberbrahmma21:02 9 Jan '07  
QuestionLicense? Pinmembermaxinuruguay15:01 4 Dec '06  
GeneralKeyboard Reference Pinmembertuantrang17:45 17 Nov '06  
GeneralTranfer Keyboard event to another application Pinmemberkhoipm16:55 7 Nov '06  
GeneralRe: Tranfer Keyboard event to another application PinmemberYoung E16:47 9 Nov '06  
GeneralRe: Tranfer Keyboard event to another application Pinmemberthietlalung19:16 31 Dec '06  
GeneralRe: Tranfer Keyboard event to another application PinmemberAKG3:37 17 Jan '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Jul 2001
Editor: Chris Maunder
Copyright 2001 by H. Joseph
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project