简体   繁体   English

Windows Unicode键盘钩

[英]Windows Unicode keyboard hook

I'm developing an application that in some point needs to capture the keyboard and mouse user input. 我正在开发一个需要捕获键盘和鼠标用户输入的应用程序。 I had no problems installing and using the mouse hook, but the keyboard is not working properly. 我在安装和使用鼠标钩时没有问题,但是键盘无法正常工作。 I need to capture wide characters from multiple keyboard layouts. 我需要从多个键盘布局中捕获宽字符。 I found something relevant but did not solve my problem: https://stackoverflow.com/questions/15976108/keyboard-hook-not-capturing-unicode-in-other-threads 我发现了一些相关的东西,但没有解决我的问题: https : //stackoverflow.com/questions/15976108/keyboard-hook-not-capturing-unicode-in-other-threads

That's what I have without the not necessary stuff 那就是我所没有的不必要的东西

WinMain: WinMain:

//Set the hook, with threadId = 0
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, hInstance, 0); 
HKL keyboardLayout = GetKeyboardLayout(0);
//All the window and message queue code

KeyboardProc: KeyboardProc:

WCHAR unicodeChar;
BYTE keyboardLayout[256];
KBDLLHOOKSTRUCT* keyParam = (KBDLLHOOKSTRUCT*) lParam;

if(code < 0)
    return CallNextHookEx(hHook, code, wParam, lParam);

if(wParam == WM_KEYDOWN)
{
    GetKeyboardState(keyboardState);

    int translation = ToUnicodeEx(keyParam->vkCode, keyParam->scanCode, keyboardState, &unicodeChar, 1, 0, keyboardLayout);
    translation = ToUnicodeEx(keyParam->vkCode, keyParam->scanCode, keyboardState, &unicodeChar, 1, 0, keyboardLayout);

    if(translation == 0 )//|| translation == -1)
        return 0;

    PushToBuffer(&unicodeChar);
}

//return CallNextHookEx(hHook, code, wParam, lParam);
return 0;

I left the commented code to show what I've already tried based on that post I mentioned and MSDN documentation. 我留下了注释的代码,以根据我提到的那篇文章和MSDN文档显示我已经尝试过的内容。 The push to buffer receives a wchar_t pointer and stores it in a wstring used as a buffer, that dumps in a wofstream. 推入缓冲区接收wchar_t指针,并将其存储在用作缓冲区的wstring中,该wstring转储到wofstream中。

What happens: 怎么了:

If I call ToUnicodeEx once, the user input is modified and any dead-key is shown twice for him: for example, in a International English Layout, where you press ' then a to print an á, it shows: ''a. 如果我一次调用ToUnicodeEx,则用户输入将被修改,并且任何死键都将显示两次:例如,在国际英语版式中,按'然后按a打印á,它显示:” a。 With the application closed the user input goes back to normal. 在应用程序关闭的情况下,用户输入恢复正常。

If I call ToUnicodeEx twice, the dead-key is consumed and I simply the char is shown. 如果我两次调用ToUnicodeEx,则死键将被消耗,而我只会显示char。

I wonder why the user input is being modified, since I do not change any of the parameters given by the callback caller. 我不知道为什么要修改用户输入,因为我没有更改回调调用者给定的任何参数。 Is unicode not possible for low-level hooks? unicode不能用于低级挂钩吗?

ToUnicodeEx is broken when it comes to dead keys. 当涉及到死键时,ToUnicodeEx已损坏。 This is what your looking for: https://code.google.com/p/jnativehook/source/browse/tags/1.1.4/src/native/windows/WinUnicodeHelper.c 这就是您要寻找的东西: https : //code.google.com/p/jnativehook/source/browse/tags/1.1.4/src/native/windows/WinUnicodeHelper.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM