即使另一个应用程序具有焦点,也检测(并覆盖)F1 键的按键

Detect (and override) keypress of F1 key even if another app has focus

在用 Visual C++ 编写的 Win32 应用程序中,如何检测 F1 和 restore/maximize 发生这种情况时的 GUI?

应该检测到此键即使另一个应用程序具有焦点,并覆盖通常的 F1 "help" window 行为。

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    const wchar_t CLASS_NAME[]  = L"Test";
    WNDCLASS wc = { };
    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;
    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"Test", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd, nCmdShow);
    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {

        // Should it be detected here ? //

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

正如@RbMm 在评论中提到的,解决方案是:

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    const wchar_t CLASS_NAME[]  = L"Test";
    WNDCLASS wc = { };
    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;
    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"Test", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd, nCmdShow);

    RegisterHotKey(hWnd, 100, 0, VK_F1);  // here we go!

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (msg.message == WM_HOTKEY)
            // Do something here

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

另见 here