Repository: microsoft/PowerToys
File: src/modules/keyboardmanager/KeyboardManagerEngineLibrary/KeyboardEventHandlers.cpp (C++)

Code before:
                        auto key = std::get<DWORD>(keyValue->second);
                        if (key == VK_LSHIFT || key == VK_RSHIFT)
                        {
                            if (state.numpadKeyPressed[it->second])
                            {
                                //replace it with original numpad
                                data->lParam->vkCode = it->second;
                            }
                        }
                    }
                    if (keyValue->second.index() == 1)
                    {
                        auto key = std::get<Shortcut>(keyValue->second);
                        if (key.shiftKey != ModifierKey::Disabled)
                        {
                            if (state.numpadKeyPressed[it->second])
                            { 
                                //replace it with original numpad
                                data->lParam->vkCode = it->second;
                            }
                        }
                    }
                }
            }
        }
        if (Helpers::IsNumpadKey(data->lParam->vkCode))
        {
            // store if the Numpad key was pressed or not. If numpad numbers were pressed but then we get the same key KEY UP but with Numpad unlocked we will replace it.
            state.numpadKeyPressed[data->lParam->vkCode] = (data->wParam == WM_KEYDOWN || data->wParam == WM_SYSKEYDOWN);
        }
    }
}

namespace KeyboardEventHandlers
{
    // Function to a handle a single key remap
    intptr_t HandleSingleKeyRemapEvent(KeyboardManagerInput::InputInterface& ii, LowlevelKeyboardEvent* data, State& state) noexcept
    {
        // Check if the key event was generated by KeyboardManager to avoid remapping events generated by us.
        if (!GeneratedByKBM(data))
        {
            UpdateNumpadWithShift(data, state);
            const auto remapping = state.GetSingleKeyRemap(data->lParam->vkCode);
            if (remapping)
            {
                auto it = remapping.value();

                // Check if the remap is to a key or a shortcut
                const bool remapToKey = it->second.index() == 0;

                // If mapped to VK_DISABLED then the key is disabled

Review comment (refactor):
```suggestion
        if (Helpers::IsNumpadKeyThatIsAffectedByShift(data->lParam->vkCode))
```

Code after:
                        auto key = std::get<DWORD>(keyValue->second);
                        if (key == VK_LSHIFT || key == VK_RSHIFT || key == VK_SHIFT)
                        {
                            if (state.numpadKeyPressed[it->second])
                            {
                                //replace it with original numpad
                                data->lParam->vkCode = it->second;
                            }
                        }
                    }
                    if (keyValue->second.index() == 1)
                    {
                        auto key = std::get<Shortcut>(keyValue->second);
                        if (key.shiftKey != ModifierKey::Disabled)
                        {
                            if (state.numpadKeyPressed[it->second])
                            { 
                                //replace it with original numpad
                                data->lParam->vkCode = it->second;
                            }
                        }
                    }
                }
            }
        }
        if (Helpers::IsNumpadKeyThatIsAffectedByShift(data->lParam->vkCode))
        {
            // store if the Numpad key was pressed or not. If numpad numbers were pressed but then we get the same key KEY UP but with Numpad unlocked we will replace it.
            state.numpadKeyPressed[data->lParam->vkCode] = (data->wParam == WM_KEYDOWN || data->wParam == WM_SYSKEYDOWN);
        }
    }
}

namespace KeyboardEventHandlers
{
    // Function to a handle a single key remap
    intptr_t HandleSingleKeyRemapEvent(KeyboardManagerInput::InputInterface& ii, LowlevelKeyboardEvent* data, State& state) noexcept
    {
        // Check if the key event was generated by KeyboardManager to avoid remapping events generated by us.
        if (!GeneratedByKBM(data))
        {
            UpdateNumpadWithShift(data, state);
            const auto remapping = state.GetSingleKeyRemap(data->lParam->vkCode);
            if (remapping)
            {
                auto it = remapping.value();

                // Check if the remap is to a key or a shortcut
                const bool remapToKey = it->second.index() == 0;

                // If mapped to VK_DISABLED then the key is disabled