将虚拟键代码转换为unicode字符 - WinApi C ++

时间:2018-01-14 20:06:18

标签: c++ winapi

我正在尝试将虚拟密钥代码转换为unicode字符。虚拟键代码是从低级键盘钩接收的。

LRESULT __stdcall hook_callback(int nCode, WPARAM wParam, LPARAM lParam)
{
        if (nCode >= 0 && wParam == WM_KEYDOWN)
        {
                const auto kbd_hook = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);

                BYTE state[256];
                GetKeyboardState(state);

                WCHAR key_buffer[2];
                ToUnicode(kbd_hook->vkCode, kbd_hook->scanCode, state, key_buffer, 2, 0);
                // handle key_buffer...
        }

        return CallNextHookEx(hook, nCode, wParam, lParam);
}

除了不考虑ShiftCaps Lock这一事实外,此工作正常。按Shift + 1将生成1,而不是预期的!(假设区域设置为美国)。但是,如果我通过单独获取每个键来填充状态缓冲区,它将按预期工作:

BYTE state[256];
for (SHORT i = 0; i < 256; ++i) {
        const SHORT key_state = GetKeyState(i);

        // Right shifts the high order bit by 8 to avoid a narrowing
        // conversion from SHORT to BYTE.
        state[i] = (key_state >> 8) | (key_state & 1);
}

如果我这样填充state缓冲区,按Shift + 1将导致!符合预期。

为什么GetKeyState()有效但GetKeyboardState()没有?

0 个答案:

没有答案