Repository: microsoft/PowerToys
File: src/modules/MouseUtils/MousePointerCrosshairs/dllmain.cpp (C++)

Code before:
            if (kb && kb->vkCode == VK_ESCAPE && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
            {
                if (auto inst = g_instance.load(std::memory_order_acquire))
                {
                    if (inst->m_enabled && inst->m_glideState.load() != 0)
                    {
                        inst->CancelGliding();
                        // Do not swallow Escape; pass it through
                    }
                }
            }
        }
        return CallNextHookEx(nullptr, nCode, wParam, lParam);
    }

    void InstallKeyboardHook()
    {
        if (m_keyboardHook)
        {
            return; // already installed
        }
        m_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, m_hModule, 0);
        if (!m_keyboardHook)
        {
            Logger::error("Failed to install low-level keyboard hook for MousePointerCrosshairs (Escape cancel). GetLastError={}.", GetLastError());
        }
    }

    void UninstallKeyboardHook()
    {
        if (m_keyboardHook)
        {
            UnhookWindowsHookEx(m_keyboardHook);
            m_keyboardHook = nullptr;
        }
    }

    // Load the settings file.
    void init_settings()
    {
        try
        {
            // Load and parse the settings file for this PowerToy.
            PowerToysSettings::PowerToyValues settings =
                PowerToysSettings::PowerToyValues::load_from_settings_file(MousePointerCrosshairs::get_key());
            parse_settings(settings);
        }
        catch (std::exception&)
        {
            Logger::error("Invalid json when trying to load the Mouse Pointer Crosshairs settings json from file.");
        }

Review comment (suggestion):
Instead of always register for this keyboardHook (i.e. always listening at anytime), I will suggest only
1. Do InstallKeyboardHook at case:
HandleGlidingHotkey when state 0 starts.
2. Do UninstallKeyboardHook at case
HandleGlidingHotkey when state 4 ends

Code after:
    }

    // Low-level keyboard hook procedures
    static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if (nCode == HC_ACTION)
        {
            const KBDLLHOOKSTRUCT* kb = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
            if (kb && kb->vkCode == VK_ESCAPE && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
            {
                if (auto inst = g_instance.load(std::memory_order_acquire))
                {
                    if (inst->m_enabled && inst->m_glideState.load() != 0)
                    {
                        inst->UninstallKeyboardHook();
                        inst->CancelGliding();
                    }
                }
            }
        }

        // Do not swallow Escape; pass it through
        return CallNextHookEx(nullptr, nCode, wParam, lParam);
    }

    void InstallKeyboardHook()
    {
        if (m_keyboardHook)
        {
            return; // already installed
        }
        m_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, m_hModule, 0);
        if (!m_keyboardHook)
        {
            Logger::error("Failed to install low-level keyboard hook for MousePointerCrosshairs (Escape cancel). GetLastError={}.", GetLastError());
        }
    }

    void UninstallKeyboardHook()
    {
        if (m_keyboardHook)
        {
            UnhookWindowsHookEx(m_keyboardHook);
            m_keyboardHook = nullptr;
        }
    }

    // Load the settings file.
    void init_settings()
    {
        try