Repository: microsoft/PowerToys
File: src/modules/poweraccent/PowerAccentKeyboardService/KeyboardListener.cpp (C++)

Code before:
        {
            if (triggerPressed == VK_LEFT)
            {
                Logger::info(L"Next toolbar position - left");
                m_nextCharCb(TriggerKey::Left, m_shiftPressed);
            }
            else if (triggerPressed == VK_RIGHT)
            {
                Logger::info(L"Next toolbar position - right");
                m_nextCharCb(TriggerKey::Right, m_shiftPressed);
            }
            else if (triggerPressed == VK_SPACE)
            {
                Logger::info(L"Next toolbar position - space");
                m_nextCharCb(TriggerKey::Space, m_shiftPressed);
            }

            return true;
        }

        return false;
    }

    bool KeyboardListener::OnKeyUp(KBDLLHOOKSTRUCT info) noexcept
    {
        if (m_shiftPressed && (info.vkCode == VK_LSHIFT || info.vkCode == VK_RSHIFT))
        {
            m_shiftPressed = false;
        }

        if (std::find(std::begin(letters), end(letters), static_cast<LetterKey>(info.vkCode)) != end(letters) && m_isLanguageLetterCb(static_cast<LetterKey>(info.vkCode)))
        {
            letterPressed = LetterKey::None;

            if (m_toolbarVisible)
            {
                if (m_stopwatch.elapsed() < m_settings.inputTime)
                {
                    Logger::info(L"Activation too fast. Do nothing.");

                    // False start, we should output the space if it was the trigger.
                    if (m_triggeredWithSpace)
                    {
                        m_hideToolbarCb(InputType::Space);
                    }
                    else
                    {
                        m_hideToolbarCb(InputType::None);
                    }

                    m_toolbarVisible = false;

Review comment (suggestion):
Let's consider the next scenario:
1. <kbd>Left Shift</kbd> is down
2. <kbd>Right Shift</kbd> is down
3. <kbd>Right Shift</kbd> is up

Then `m_shiftPressed` will be set to false, even though <kbd>Left Shift</kbd> is still pressed.




Code after:
            CharUpperBuffW(processPath.data(), (DWORD)processPath.length());
            m_prevForegrndAppExcl = { foregroundApp,
                                      find_app_name_in_path(processPath, m_settings.excludedApps) };

            return m_prevForegrndAppExcl.second;
        }

        m_prevForegrndAppExcl = { NULL, false };

        return false;
    }

    bool KeyboardListener::OnKeyDown(KBDLLHOOKSTRUCT info) noexcept
    {
        auto letterKey = static_cast<LetterKey>(info.vkCode);

        // Shift key is detected only if the toolbar is already visible to avoid conflicts with uppercase
        if (!m_leftShiftPressed && m_toolbarVisible && info.vkCode == VK_LSHIFT)
        {
            m_leftShiftPressed = true;
        }

        if (!m_rightShiftPressed && m_toolbarVisible && info.vkCode == VK_RSHIFT)
        {
            m_rightShiftPressed = true;
        }

        if (std::find(letters.begin(), letters.end(), letterKey) != cend(letters) && m_isLanguageLetterCb(letterKey))
        {
            m_stopwatch.reset();
            letterPressed = letterKey;
        }

        UINT triggerPressed = 0;
        if (letterPressed != LetterKey::None)
        {
            if (std::find(std::begin(triggers), end(triggers), static_cast<TriggerKey>(info.vkCode)) != end(triggers))
            {
                triggerPressed = info.vkCode;

                if ((triggerPressed == VK_SPACE && m_settings.activationKey == PowerAccentActivationKey::LeftRightArrow) ||
                    ((triggerPressed == VK_LEFT || triggerPressed == VK_RIGHT) && m_settings.activationKey == PowerAccentActivationKey::Space))
                {
                    triggerPressed = 0;
                    Logger::debug(L"Reset trigger key");
                }
            }
        }

        if (!m_toolbarVisible && letterPressed != LetterKey::None && triggerPressed && !IsForegroundAppExcluded())
        {