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_CLICKACTIVEmeans mouse activation -
WA_ACTIVEmeans 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.