Sunday, November 6, 2011

Mangling Keys of Interaction With Hardware on C programming

How about one more program to bolster your confidence? Let us try one that would mangle every key that is pressed. That is, convert an A to a B, B to C, C to D, etc. This would be fairly straight-forward. We simply have to increment the key code before posting it into the system message queue. Also, further processing of key has to be prevented. This can be achieved by simply returning a non-zero value from the hook procedure (thus bypassing the call to CallNextHookEx( )). This is shown in the following hook procedure.

LRESULT __declspec ( dllexport ) __stdcall KeyboardProc ( int nCode, 
                                                       WPARAM wParam, LPARAM lParam )
{
static BYTE key ;
static BOOL flag = FALSE ;
if ( nCode < 0 )
return CallNextHookEx ( hkb, nCode, wParam, lParam ) ;
if ( ( nCode == HC_ACTION ) &&
( ( DWORD ) lParam & 0x80000000 ) == 0 )
{
if ( flag == FALSE )
{
key = wParam ;
key ++ ;
keybd_event ( key , 0, KEYEVENTF_EXTENDEDKEY, 0 ) ;
flag = TRUE ;
return 1 ;
}
else
{
if ( key == ( BYTE ) wParam )
flag = FALSE ;
}
}
return CallNextHookEx ( hkb, nCode, wParam, lParam ) ;
}

No comments:

Post a Comment