Search code examples
c++winapikeypress

Mapping of virtual keycode to unicode influenced by writing to std::cout?


I am running into an odd issue which I don't really know how to troubleshoot. I am writing code in C++ which is supposed to trigger events when keys are pressed. I can detect keypress and release fine, using GetAsyncKeystate, but I cannot reliably convert the detected keystates into unicode. (note that I am using Qt, but that shouldn't really matter)

My press / release detection:

// This function is called in a loop
void KeyboardApi::Check()
{
    bool stat = false;
    for (int i = 7; i < 255; i++) { // 0 undefined, 3 VK_CANCEL can be ignored, 1 2 4 5 6 mouse keys, these are ignored
        stat = ((GetAsyncKeyState(i) & 0x8000) != 0);

        if (this->first_time) { // To prevent reporting keypresses upon initialization
            this->first_time = false;
            this->keystate[i] = stat;
            continue;
        }

        if (stat != this->keystate[i]) {
            this->keystate[i] = stat;

            if (i == VK_SHIFT)
                this->shift = stat;
            else if (i == VK_CONTROL)
                this->ctrl = stat;
            else if (i == VK_MENU)
                this->alt = stat;

            HKL locale = GetKeyboardLayout(GetCurrentThreadId());

            // ---!! If this portion is commented, the code does not work correctly.
            //       if this portion is *not* commented, the code works fine...
            /*
            std::wcout << "args for keycode_to_unicode:" << std::endl
                       << "  keycode: " << i << std::endl
                       << "  locale: " << locale << std::endl
                       << "  shift: " << this->shift << std::endl;
            */
            // ---!!

            QString key_string = keycode_to_unicode(i, locale, this->shift);

            if (stat)
                std::wcout << "Key pressed:  " << i <<  " unicode: " << key_string.toStdWString() << std::endl;
        }
    }
}

And the keycode_to_unicode function:

QString keycode_to_unicode(unsigned int key, HKL keyboardLayoutHandle, bool shiftPressed)
{
    int scanCodeEx = MapVirtualKeyExW(key, MAPVK_VK_TO_VSC_EX, keyboardLayoutHandle);

    if (scanCodeEx > 0) {
        unsigned char lpKeyState[256];

        if (shiftPressed) {
            lpKeyState[VK_SHIFT] = 0x80;
            lpKeyState[VK_LSHIFT] = 0x80;
        }

        wchar_t buffer[5];

        int rc = ToUnicodeEx(key, scanCodeEx, lpKeyState, buffer, 5, 0, keyboardLayoutHandle);

        if (rc > 0) {
            return QString::fromWCharArray(buffer);
        } else {
            // It's a dead key; let's flush out whats stored in the keyboard state.
            rc = ToUnicodeEx(key, scanCodeEx, lpKeyState, buffer, 5, 0, keyboardLayoutHandle);
            return QString();
        }
    }

    return QString();
}

So the odd thing is that KeyboardApi::Check() works fine when I have the debug output there, but when I don't have it the conversion to unicode goes wrong. For example, when I press the 'A' key the first time, 'a' is outputted. The second time, 'β', 'β', etc.

EDIT: You may ask yourself why I am not using Qt's build-in onKeyPress... This is because my code is injected into other processes, thus I cannot use this approach.


Solution

  • It turned out to be an issue with keycode_to_unicode!

    In my code, I had:

    unsigned char lpKeyState[256];
    

    Which means the array is not initialized; its contents could be anything. Replacing it with:

    unsigned char lpKeyState[256] = {0};
    

    Fixed the issue of odd input. It turned out I also had some other issues (specifically with dead-keys). I altered my code a bit and am now able to properly detect dead-keys when they are pressed, though I have not succeeded yet in combining the dead-keys with follow-up characters to detect characters with accents. For the sake of completeness, here follows the modified code:

    Note: it does not take into account capslock.

    QString KeyboardApi::keycode_to_unicode(unsigned int key, HKL keyboardLayoutHandle, bool shiftPressed, bool ctrlPressed, bool altPressed, bool capslock)
    {
        int scanCodeEx = MapVirtualKeyExW(key, MAPVK_VK_TO_VSC_EX, keyboardLayoutHandle);
    
        if (scanCodeEx <= 0)
            return QString();
    
        unsigned char lpKeyState[256] = {0};
    
        if (shiftPressed) {
            lpKeyState[VK_SHIFT] = 0x80;
            lpKeyState[VK_LSHIFT] = 0x80;
        }
    
        if (ctrlPressed)
            lpKeyState[VK_CONTROL] = 0x80;
    
        if (altPressed)
            lpKeyState[VK_MENU] = 0x80;
    
        if (capslock)
            lpKeyState[VK_CAPITAL] = 0x01;
    
        wchar_t buffer[5];
    
        ToUnicodeEx(key, scanCodeEx, lpKeyState, buffer, 5, 0, keyboardLayoutHandle);
    
        QString ret = QString::fromWCharArray(buffer);
        ret.truncate(1);
    
        return ret;
    }
    
    Qt::Key KeyboardApi::unicode_to_qtkey(QString key)
    {
        unsigned int code = key[0].unicode();
        if (key[0].unicode() >= 'a' && key[0].unicode() <= 'z') {
            code = key[0].unicode() - 'a' + Qt::Key_A;
        }
    
        return (Qt::Key)code;
    }
    
    void KeyboardApi::Check()
    {
        bool init = this->first_time;
    
        if (this->first_time)
            this->first_time = false;
    
        bool stat = false;
        for (int i = 7; i < 255; i++) { // yes, 255 is NOT valid! // 0 undefined, 3 VK_CANCEL can be ignored, 1 2 4 5 6 mouse keys
            stat = ((GetAsyncKeyState(i) & 0x8000) != 0);
    
            if (stat == this->keystate[i])
                continue;
    
            this->keystate[i] = stat;
    
            if (i == VK_SHIFT)
                this->shift = stat;
            else if (i == VK_CONTROL)
                this->ctrl = stat;
            else if (i == VK_MENU)
                this->alt = stat;
            else if (i == VK_CAPITAL)
                this->capslock = stat;
    
            HKL locale = GetKeyboardLayout(GetCurrentThreadId());
    
            QString key_string = keycode_to_unicode(i, locale, this->shift, this->ctrl, this->alt, false);
    
            // See qkeymapper_win.cpp for the table used to convert windows virtual key codes to Qt::Key
            Qt::Key key = key_string.isEmpty() ? (Qt::Key)win_vk_to_qt_key[i] : unicode_to_qtkey(key_string);
    
            if (init || i == VK_SHIFT || i == VK_CONTROL || i == VK_MENU)
                continue;
    
            if (stat) {
                emit this->OnKeyDown(this, QKeyEvent(QEvent::KeyPress, key, mods, key_string));
            } else
                emit this->OnKeyUp(this, QKeyEvent(QEvent::KeyPress, key, mods, key_string));
        }
    }