-1

I'm trying to find a way to detect when my own window is being activated from Alt+Tab.

I thought about using CBTProc but HCB_ACTIVATE doesn't give this info, also using a LowLevelKeyboard hook, but I would like to avoid hooking.

While searching more about I found that there's an API that stores this information SwitchToThisWindow

Type: BOOL

A TRUE for this parameter indicates that the window is being switched to using the Alt/Ctl+Tab key sequence. This parameter should be FALSE otherwise.

But this API is used by the taskbar, I wonder if is possible to get this information into my window.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Cesar
  • 41
  • 2
  • 5
  • 16
  • What's wrong with [`WM_ACTIVATE`](https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-activate) (Check the __Parameters__ section) _"...Sent to both the window being activated and the window being deactivated. ..."_ ? – Richard Critten Aug 15 '22 at 20:36
  • @RichardCritten How does `WM_ACTIVATE` tells me if the window is being activated from alt + tab? `WA_ACTIVE` is also true when you minimize another window that is above your window. – Cesar Aug 15 '22 at 20:37
  • I don't see how `SwitchToThisWindow` does what you want, `[in] fUnknown` never meant to be output. – apple apple Aug 15 '22 at 20:41
  • Yes, i misread it – Cesar Aug 15 '22 at 20:44
  • 2
    `WA_ACTIVE` comes close. But this is a strange requirement, what underlies it? – Paul Sanders Aug 15 '22 at 20:48
  • 4
    Why is activation by `Alt+Tab` important to you ? There are quite a few ways to activate a Window (application) eg `Alt+Esc`, from Task Manager, from Task Bar, minimising other windows until yours becomes active etc ... . This feels like an XY-Problem. – Richard Critten Aug 15 '22 at 20:48

1 Answers1

0

SetWinEventHook() can tell you when the user is invoking the Alt+Tab switcher, via the EVENT_SYSTEM_SWITCHSTART and EVENT_SYSTEM_SWITCHEND events:

https://learn.microsoft.com/en-us/windows/win32/winauto/event-constants

Constant/value Description
EVENT_SYSTEM_SWITCHEND
0x0015
The user has released ALT+TAB. This event is sent by the system, never by servers. The hwnd parameter of the WinEventProc callback function identifies the window to which the user has switched.
If only one application is running when the user presses ALT+TAB, the system sends this event without a corresponding EVENT_SYSTEM_SWITCHSTART event.
EVENT_SYSTEM_SWITCHSTART
0x0014
The user has pressed ALT+TAB, which activates the switch window. This event is sent by the system, never by servers. The hwnd parameter of the WinEventProc callback function identifies the window to which the user is switching.
If only one application is running when the user presses ALT+TAB, the system sends an EVENT_SYSTEM_SWITCHEND event without a corresponding EVENT_SYSTEM_SWITCHSTART event.

UPDATE: apparently, these events don't work anymore since Windows 10 onward. In which case, you have to resort to a dirty hack that correlates events between SetWindowsHookEx(WH_KEYBOARD_LL) and SetWinEventHook(EVENT_SYSTEM_FOREGROUND), eg:

The system events EVENT_SYSTEM_SWITCHSTART and EVENT_SYSTEM_SWITCHEND works in Windows 7 but not in Windows 10

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • This events no more work in latest windows versions. More exactly, when alt+tab handler was inside explorer ( by default) this events not sent. If move handler to csrss ( this possible do if attach debugger to explorer and press alt+tab explorer deregister self hot key handler - it check for debugger during handle alt+tab). Who is handle csrss or explorer visible in ui - pictures from csrss more classic and minimal, compare modern ui from explorer – RbMm Aug 15 '22 at 22:53