views:

350

answers:

2

I am trying to completely disable the letter 'a' on the keyboard using lowlevel keyboard hook. The problem is that when i return 0 from keyboardproc the key is not disabled but when i return 1 it gets disabled. I thought that returning from keyboardproc without calling CallNextHookEx blocks the message. Is there any difference between returning 0 and returning 1.

LRESULT CALLBACK LowLevelKeyboardProc(int code, WPARAM wParam, LPARAM lParam) {

KBDLLHOOKSTRUCT* details = (KBDLLHOOKSTRUCT*) lParam;

if(code == HC_ACTION && wParam == WM_KEYDOWN)
{
 if(details->vkCode == 0x41)
 {
  return 1;
 }
}

return CallNextHookEx(g_hhkKeyboard, code, wParam, lParam);

}

+2  A: 

From the LowLevelKeyboardProc MSDN documentation:

If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.

[emphasis mine]

So it's not entirely well documented, what happens if you actually return 0. I'd guess this value causes the system to call the next hook procedure in the chain anyway, and it eventually gets processed by your default window procedure.

I have recently written a simple app that requires you to hold Caps Lock for a given amount of time in order to toggle it, and whenever I actually handle this key in my LowLevelKeyboardProc, I always return 1, otherwise

CallNextHookEx( NULL, nCode, wParam, lParam );

Additional note: If you're targetting NT/XP/2003 and newer systems, you can pass NULL in the first CallNextHookEx parameter, since it's ignored.

macbirdie
A: 

If you CallNextHook() the next hook in the chain is called. If you return non-zero the message is not received by the target window (thus preventing the window from receiving the keyboard message). If you return zero the message is received by the target window.

Your desired behavior is to return non-zero if the key pressed is 'a', and zero otherwise.