Share via

how to know if the win32 window is Activated using keyboard keys

Omar Mohamed 120 Reputation points
2026-04-02T10:19:57.3966667+00:00

Hello,

I have a win32 window with a window proc an event Handler and I want to know if that window is Activated using the Mouse or the Keyboard for the mouse I have used the WM_MOUSEACTIVATE but I don't know how to do it with the keyboard also how to get the Key Code for the pressed button.

I'm using c++

Thanks for everyone,

Windows development | Windows API - Win32

Answer accepted by question author
  1. Jack Dang (WICLOUD CORPORATION) 16,115 Reputation points Microsoft External Staff Moderator
    2026-04-03T06:06:40.09+00:00

    Hi @Omar Mohamed ,

    Thanks for reaching out.

    For this scenario, the main message to look at is WM_ACTIVATE.

    Win32 provides a way to distinguish mouse-click activation from non-mouse activation through the low word of wParam:

    • WA_INACTIVE: the window is being deactivated
    • WA_ACTIVE: the window is being activated by something other than a mouse click (for example, keyboard navigation or system-driven activation)
    • WA_CLICKACTIVE: the window is being activated by a mouse click

    So if your goal is to tell whether the window was activated by the mouse or not, WM_ACTIVATE already provides that information.

    Example:

    case WM_ACTIVATE:
    {
        UINT state = LOWORD(wParam);
    
        if (state == WA_CLICKACTIVE)
        {
            // Activated by mouse click
        }
        else if (state == WA_ACTIVE)
        {
            // Activated by a non-mouse mechanism
            // For example: keyboard navigation, Alt+Tab, or programmatic activation
        }
        else if (state == WA_INACTIVE)
        {
            // Deactivated
        }
        return 0;
    }
    

    For the keyboard part, the limitation is that Win32 does not provide a reliable way for the activated window to determine exactly which key caused the activation.

    So:

    • WA_CLICKACTIVE means mouse activation
    • WA_ACTIVE means non-mouse activation

    But WA_ACTIVE does not mean keyboard only. It can also be caused by things like Alt+Tab or programmatic activation.

    If you want the key code for key presses that your window actually receives, you can handle WM_KEYDOWN and WM_SYSKEYDOWN:

    case WM_KEYDOWN:
    {
        UINT vk = static_cast<UINT>(wParam); // virtual-key code
        return 0;
    }
    
    case WM_SYSKEYDOWN:
    {
        UINT vk = static_cast<UINT>(wParam); // system key, such as Alt-related input
        return 0;
    }
    

    For example, the wParam value uses virtual-key codes:

    • VK_TAB
    • VK_RETURN
    • VK_SPACE
    • VK_F1

    One thing to be aware of is that if the user activates your window through something like Alt+Tab, your window may not receive the corresponding key message directly, because that input may be handled before your window becomes active.

    Hope this helps! If my answer was helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-04-02T10:20:12.48+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Use the activation message and its parameters; do not look for a separate “keyboard activation” message.

    1. Detect whether activation was by mouse or not

    When the window is activated or deactivated, it receives a WM_ACTIVATE message:

    case WM_ACTIVATE:
    {
        WORD state = LOWORD(wParam);   // WA_ACTIVE, WA_CLICKACTIVE, WA_INACTIVE
        HWND hwndOther = (HWND)lParam; // other window
    
        if (state == WA_CLICKACTIVE)
        {
            // Activated by mouse click
        }
        else if (state == WA_ACTIVE)
        {
            // Activated by keyboard, Alt+Tab, programmatically, etc.
        }
        else if (state == WA_INACTIVE)
        {
            // Deactivated
        }
        return 0;
    }
    

    According to WM_ACTIVATE, the low-order word of wParam tells how the window was activated:

    • WA_CLICKACTIVE: activated by a mouse click.
    • WA_ACTIVE: activated by some method other than a mouse click (keyboard, SetActiveWindow, etc.).
    • WA_INACTIVE: window is being deactivated.

    So to know if the window was activated using the keyboard, check for WA_ACTIVE (and not WA_CLICKACTIVE).

    If the activation was by mouse click, the window also receives WM_MOUSEACTIVATE. For keyboard activation, there is no WM_MOUSEACTIVATE; only WM_ACTIVATE is relevant.

    1. Getting the key code for the pressed key

    Keyboard input is delivered as keystroke messages to the window that has the keyboard focus:

    • WM_KEYDOWN
    • WM_KEYUP
    • WM_SYSKEYDOWN
    • WM_SYSKEYUP

    The virtual-key code is in wParam of these messages:

    case WM_KEYDOWN:
    {
        WPARAM vk = wParam; // virtual-key code
        // Example: check for Enter key
        if (vk == VK_RETURN)
        {
            // Handle Enter
        }
        return 0;
    }
    

    If the activation came from a specific key (for example, Alt+Tab or a shortcut), the system does not give a dedicated “activation key” parameter in WM_ACTIVATE. To know which key is currently down at the time of another message (for example, a mouse click), use GetKeyState:

    if (GetKeyState(VK_MENU) & 0x8000)
    {
        // ALT key is down at the time of this message
    }
    

    GetKeyState returns a virtual keyboard state snapshot corresponding to the time the message was queued; the high bit (0x8000) indicates the key is down.

    To get the immediate physical state of a key regardless of the message queue, use GetAsyncKeyState instead.

    Summary:

    • Use WM_ACTIVATE and test LOWORD(wParam):
      • WA_CLICKACTIVE → mouse activation.
      • WA_ACTIVE → keyboard or non-mouse activation.
    • Use WM_KEYDOWN/WM_SYSKEYDOWN and wParam to get the virtual-key code.
    • Optionally use GetKeyState/GetAsyncKeyState to query key state at a given time.

    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.