C++ 键盘挂钩 - 参数 nCode 是什么意思?

C++ keyboard hook - what does the parameter nCode mean?

我正在寻找为 C++ 项目编写键盘挂钩。我找到了一些代码,但不想在没有完全理解它的情况下使用它:

HHOOK _hook;
KBDLLHOOKSTRUCT kbdStruct;
LRESULT __stdcall HookCallback(int ncode, WPARAM wParam, LPARAM lparam)
{
    if(ncode>=HC_ACTION)
    {
        if((wParam == WM_KEYDOWN) || (wParam == WM_SYSKEYDOWN))
        {
            kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
            DWORD dwMsg = 1;
            dwMsg += kbdStruct.scanCode << 16;
            dwMsg += kbdStruct.flags << 24;
            char key[16];
            GetKeyNameText(dwMsg,key,15);
            
            if((GetKeyNameState(VK_CAPITAL)& 0x0001) == 0)
            {
                for (int i=0; i<10)key[i] = tolower(key[i]);
                ReturnKeyPressed(key);
            }
            else
            {
                ReturnKeyPressed(key);
            }
        }
    }
    return CallNextHookEx(_hook,nCode,wParam,lParam);
}
void SetHook()
{
    _hook = SetWindowsHookEx(WH__KEYBOARD_LL,HookCallback,Null,0);
}

我不明白这里的 nCode 是什么。其他参数从何而来? 来自绝对 C++ 初学者的问候 :).

简而言之,nCode 告诉您 wParamlParam 是否包含有效数据。如果 nCodeHC_ACTION (0),那么他们会这样做,否则他们不会。这在 the documentation:

中有明确说明
  • nCode [in]
    Type: int

    A code the hook procedure uses to determine how to process the message. If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx. This parameter can be one of the following values.

    HC_ACTION
    0

    The wParam and lParam parameters contain information about a keyboard message.

  • wParam [in]
    输入:WPARAM

    键盘消息的标识符。此参数可以是以下消息之一:WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP.

  • lParam [in]
    输入:LPARAM

    指向 KBDLLHOOKSTRUCT 结构的指针。