With the power of windows hooks below your belt you are into the league of power programmers of Windows. So how about tasting the power some bit more. How about writing a program that would make every key pressed in any Windows application appear twice. Here is the code for the hook procedure.
LRESULT __declspec ( dllexport ) __stdcall KeyboardProc ( int nCode,
WPARAM wParam, LPARAM lParam )
{
static BYTE key ;
static BOOL flag = FALSE ;
{
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 ;
keybd_event ( key , 0, KEYEVENTF_EXTENDEDKEY, 0 ) ;
flag = TRUE ;
}
else
{
if ( key == ( BYTE ) wParam )
flag = FALSE ;
}
}
return CallNextHookEx ( hkb, nCode, wParam, lParam ) ;
}
In this hook procedure once again we have checked if the nCode parameter contains a value HC_ACTION. If it does then we have checked the present state of the key in question. If the present state of the key is ‘pressed’ (31th bit of lParam is 0) then we have posted the message for the same key into the system message queue by calling the keybd_event( ). However, this may lead to a serious problem. Can you imagine which? The message that we post, once retrieved, would again bring the control to our hook procedure. Once again the conditions would become true and we would post the same message again. This would go on and on. This can be prevented by using a using a simple flag variable as shown in the code.
Note that the rest of the functions in the DLL file are exactly same as in the previous program. So also is the application program
No comments:
Post a Comment