除非我有一个messgaebox(),否则C ++ ToUnicodeEx()无法识别Shift键 [英] C++ ToUnicodeEx() not recognizing shift key unless i have a messgaebox()

查看:95
本文介绍了除非我有一个messgaebox(),否则C ++ ToUnicodeEx()无法识别Shift键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用C ++编写键盘挂钩时遇到了麻烦.

im having a bit of trouble writing a keyboard hook in C++.

我可以读取击键,但是我想在按下Shift键时尝试使用ToUnicodeEx()转换击键. 到目前为止,我在代码中拥有

I can read keystrokes but im trying to using ToUnicodeEx() to convert keystrokes when shift key is pressed. In the code i have so far i have

i = ToUnicodeEx(keyboard.vkCode, keyboard.scanCode, (PBYTE)&keyState, (LPWSTR)&keybuff, sizeof(keybuff) / 16, 0,keyboardlayout);
MessageBox(MainnhWnd,keybuff, L"message", MB_OK | MB_ICONEXCLAMATION);

当我按Shift + 2时出现此'MessageBox'行,我弹出两个消息框,第一个为Shift键为空白,第二个为'@'字符.这是预期的.

with this 'MessageBox' line when I press Shift+2 i get two message boxes pops up, the first is blank for the shift key, the second shows a '@' character. This is expected.

但是,如果我删除此消息框,则ToUnicodeEx()函数将转换击键,就好像未使用shift键一样.通过设置断点,命中计数器或将字符输出到程序窗口中的编辑框,我可以看到这一点.

But if i remove this messagebox, the ToUnicodeEx() function converts the keystroke as if the shift key had not been used. I can see this by either setting a breakpoint, with a hit counter, or outputting the character to an edit box in my program windows.

另外,当我包含MsgBox行并使用CapLock时,我的字母也会相应更改,但是当我删除msgbox行后,它仅在程序启动时使用大写锁定的状态(当programm启动时,大写锁定处于打开状态,所有字母均为大写字母,反之亦然,即使在程序启动时,所有字母都很小,即使我更改了大写锁定状态也是如此

Also when i include the MsgBox line and use the CapLock, my letters change accordingly but after i remove the msgbox line it only uses the state of the cap lock at the time my program starts (cap locks is on when programm starts, all letters are capital, vice verse, caps off when programm starts all letters are small,, even if i change the cap lock state)

任何人都知道为什么我的钩子只记得启动时的键盘状态,除非我包括msgbox?

Anyone know why my hook just remembers the keyboard state at start, unless i include the msgbox?

我的钩子设置为:

theHook = SetWindowsHookEx ( WH_KEYBOARD_LL, (HOOKPROC) KeyEvent, exe, 0);

我的钩子回调函数是:

DLLEXPORT LRESULT CALLBACK KeyEvent(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode>=0) {
    int i = 0;
    KBDLLHOOKSTRUCT keyboard;
    WCHAR keybuff[256]= {0};

    if ((wParam == WM_KEYDOWN)|| (wParam == WM_SYSKEYDOWN)||(wParam == WM_SYSKEYUP)) {
        keyboard = *((KBDLLHOOKSTRUCT*)lParam);
        if (keyboard.vkCode == VK_RETURN) {
            i += wsprintf (((LPWSTR)keybuff + i),L"[Return]\r\n");
        }
        else {
            HKL keyboardlayout = GetKeyboardLayout(0);
            GetKeyboardState((PBYTE)&keyState);
            i = ToUnicodeEx(keyboard.vkCode, keyboard.scanCode, (PBYTE)&keyState, (LPWSTR)&keybuff, sizeof(keybuff) / 16, 0,keyboardlayout);

            MessageBox(MainnhWnd,keybuff, L"message", MB_OK | MB_ICONEXCLAMATION);
        }
        if (keybuff>0) {
            addToEditBox(keybuff);
        }
    }
}
return CallNextHookEx(theHook, nCode, wParam, lParam);
}

推荐答案

根据

According to documentation of ToUnicodeEx function, you should provide a pointer to a 256-byte array that contains the current keyboard state (const BYTE *lpKeyState). Each element (byte) in the array contains the state of one key. If the high-order bit of a byte is set, the key is down.

在调用ToUnicodeEx之前,应像下面这样设置此数组(伪代码):

Before you call ToUnicodeEx, you should set this array like this (pseudocode):

enum Keys
{
    ShiftKey    = 16, // shift
    ControlKey  = 17, // ctrl
    Menu        = 18, // alt
    Capital     = 20, // caps lock
};

BYTE keyState[256]= {0};

if (Control key down)
    keyState[Keys::ControlKey] = 0x80;

if (Shift key down)
    keyState[Keys::ShiftKey] = 0x80;

if (Alt key down)
    keyState[Keys::Menu] = 0x80;

if (Caps lock ON)
    keyState[Keys::Capital] = 0x01;

设置了keyState数组后,您可以调用:

And when keyState array is set, then you can call:

ToUnicodeEx(keyboard.vkCode, keyboard.scanCode, (PBYTE)&keyState, (LPWSTR)&keybuff, sizeof(keybuff) / 16, 0,keyboardlayout);

我一直在使用ToUnicodeEx函数,并且效果很好.
希望这会对您有所帮助;)

I have been working with ToUnicodeEx function just like this and it worked fine.
Hope this will help you ;)

这篇关于除非我有一个messgaebox(),否则C ++ ToUnicodeEx()无法识别Shift键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆