Disabling Keys with Window Hooks

I am trying to completely disable the letter "a" on the keyboard using a low-level keyboard. The problem is that when I return 0 from the keyboard the key is not disabled, but when I return 1 it is disabled. I thought returning from the keyboard without calling CallNextHookEx would block 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);     
}

      

+1


a source to share


2 answers


From LowLevelKeyboardProc

MSDN Documentation
:

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

[my accent]

So it's not exactly well-documented what happens if you really are return 0

. My guess is that this value causes the system to call the next hook procedure in the chain anyway, and it ends up being handled by your default window procedure.



I recently wrote a simple app that requires you to hold the Caps Lock for a certain amount of time in order to toggle it, and whenever I actually process that key in mine LowLevelKeyboardProc

, I always return 1, otherwise

CallNextHookEx( NULL, nCode, wParam, lParam );

      

Additional note. If you are targeting NT / XP / 2003 and newer systems, you can pass NULL

in the first parameter CallNextHookEx

as it is ignored.

+3


a source


If you call CallNextHook (), the next hook in the chain is called. If you return a nonzero value, the message will not be received by the target window (so that the window will not receive a keyboard message). If you return zero, the message will be received by the target window.



Your desired behavior is to return nonzero if the "a" key is pressed, and zero otherwise.

0


a source







All Articles