|
-
April 3rd, 2007, 06:13 AM
#1
Low Level Keyboard Hook problem
Hi Folks,
I'm currently writting a LLKeyboard hook, my goal is to redirect all inputs just to one window, regardless what other applications are running.
I made it acutall working so my problem is more trivial:
------SNIPPET-------
Code:
LRESULT CALLBACK LLHook(int nCode, WPARAM wParam, LPARAM lParam){
KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lParam;
BOOL bCtrl = GetAsyncKeyState(VK_CONTROL)>>((sizeo(SHORT) * 8) - 1);
BOOL bShift = GetAsyncKeyState(VK_SHIFT)>>((sizeof(SHORT) * 8) - 1);
if (nCode==HC_ACTION) {
if(GetFocus() == hWndredirect){
if(!bCtrl && !bShift){
PostMessage(GetFocus(),WM_KEYDOWN,pkh->vkCode,pkh->scanCode);
//PostMessage(GetFocus(),WM_KEYUP,pkh->vkCode,(LPARAM)0);
}
}
else SetFocus(hWndredirect);
}
return(-1);
}
------SNIPPET-------
This code works well, my only problem is that hWndredirect receives all keys twice. You type "a", "aa" appears and so on, I outcommented the WM_KEYUP message already, without that it was "aaaa".
My guess is that there probably something is wrong with the scanCode but I can't figure out the problem really.
Thanks in Advance, Andy
Last edited by AndyTheAce; April 3rd, 2007 at 06:17 AM.
-
April 3rd, 2007, 07:02 AM
#2
Re: Low Level Keyboard Hook problem
Your problem is that in the hook you don’t distinguish between Down/Up stroke whenever you enter the hook you sending WM_KEYDOWN message first for the down and second for the up.
Code:
if (nCode==HC_ACTION)
{
if(GetFocus() == hWndredirect)
{
if(!bCtrl && !bShift)
{
if ( wParam == WM_KEYDOWN)
PostMessage(GetFocus(),WM_KEYDOWN,pkh->vkCode,pkh->scanCode);
else if ( wParam == WM_KEYUP )
PostMessage(GetFocus(),WM_KEYUP,pkh->vkCode,(LPARAM)0);
}
}
else SetFocus(hWndredirect);
}
Just bear in mind that there are other events like WM_SYSKEYDOWN/WM_SYSKEYUP.
Cheers
-
April 3rd, 2007, 07:28 AM
#3
Re: Low Level Keyboard Hook problem
Thanks for reply, you were right I forgot to catch that.
But its still not really good... when I pass the scanCode the result
is a senseless number of characters.. but when I pass (LPARAM)0 insteat, it works fine.
Any explanation for that?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|