hello,
I am new to the window hook development area. I found the following code in
a link,i am not able to understand this. Please explain me wat this does.
The code has the KeyEventArgs and this is sent as an input to events like
KeyDown, KeyUp and KeyPress. Wat do this events do. I dont find any
definitions for these events.
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
// it was ok and someone listens to events
if ((nCode >= 0) && (KeyDown!=null || KeyUp!=null || KeyPress!=null))
{
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct) Marshal.
PtrToStructure(lParam, typeof(KeyboardHookStruct));
// raise KeyDown
if ( KeyDown!=null && ( wParam ==WM_KEYDOWN || wParam==WM_SYSKEYDOWN ))
{
Keys keyData=(Keys)MyKeyboardHookStruct.vkCode;
KeyEventArgs e = new KeyEventArgs(keyData);
KeyDown(this, e);
}
// raise KeyPress
if ( KeyPress!=null && wParam ==WM_KEYDOWN )
{
byte[] keyState = new byte[256];
GetKeyboardState(keyState);
byte[] inBuffer= new byte[2];
if (ToAscii(MyKeyboardHookStruct.vkCode,
MyKeyboardHookStruct.scanCode,
keyState,
inBuffer,
MyKeyboardHookStruct.flags)==1)
{
KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0])
;
KeyPress(this, e);
}
}
// raise KeyUp
if ( KeyUp!=null && ( wParam ==WM_KEYUP || wParam==WM_SYSKEYUP ))
{
Keys keyData=(Keys)MyKeyboardHookStruct.vkCode;
KeyEventArgs e = new KeyEventArgs(keyData);
KeyUp(this, e);
}
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
Hopefully someone can explain me this.
Thanks and regards,
hemapriya