Keybaord hooking
-
Hi, i am using the function SetWindowsHookEx() to set the keyboard hook. The code snippet is as follows
if(myHook == NULL)
myHook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)myFunction, AfxGetApp()->m_hInstance, NULL);LRESULT myFunction( int ncode, WPARAM wparam, LPARAM lparam)
{
if((wparam == VK_F10))
{
DoSomething();
}
return ( CallNextHookEx(myHook,ncode,wparam,lparam) );
}The problem is that the function myFunction() is getting called two times. Can someone tell me how can i avoid this. Thanks
-
Hi, i am using the function SetWindowsHookEx() to set the keyboard hook. The code snippet is as follows
if(myHook == NULL)
myHook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)myFunction, AfxGetApp()->m_hInstance, NULL);LRESULT myFunction( int ncode, WPARAM wparam, LPARAM lparam)
{
if((wparam == VK_F10))
{
DoSomething();
}
return ( CallNextHookEx(myHook,ncode,wparam,lparam) );
}The problem is that the function myFunction() is getting called two times. Can someone tell me how can i avoid this. Thanks
ashtwin wrote:
The problem is that the function myFunction() is getting called two times.
This is normal behavior. When you press a key on your keyboard it generates a keypress and release and if you hold the key down it generates repeat keys. You should probably have a look at how the the KeyboardProc [^]hook procedure is implemented. And if you read About Keyboard Input[^] you will find that you can check the transition state of the key with something like:
if (HIWORD(lparam) & KF_UP) { //The key was released. }
Best Wishes, -David Delaune -
ashtwin wrote:
The problem is that the function myFunction() is getting called two times.
This is normal behavior. When you press a key on your keyboard it generates a keypress and release and if you hold the key down it generates repeat keys. You should probably have a look at how the the KeyboardProc [^]hook procedure is implemented. And if you read About Keyboard Input[^] you will find that you can check the transition state of the key with something like:
if (HIWORD(lparam) & KF_UP) { //The key was released. }
Best Wishes, -David Delaune