The ALT + Tab key combination in Windows is used to switch between programs. Therefore, the keyboard message (WM_KEYDOWN/UP) is sent directly to the system kernel and is not intercepted in the message loop in the application.
One common question is, but what happens when some apps want to do something between the ALT + TAB switch to the background?
One solution is to use the underlying keyboard hooks to intercept the entire system's keyboard input. But doing so can lead to some efficiency and stability issues.
Another convenient and safe solution is to use the Windows accessbility API setwineventhook function to monitor Event_system_switchstart and Event_system_switchend events.
These 2 events are for the application user to press the ALT + TAB key and release the ALT + TAB key, the following is an explanation of MSDN:
The user has pressed ALT + TAB, which activates the switch window. This event was sent by the system and never by servers. The HWND parameter of the WINEVENTPROC callback function identifies the window to which the user is switching. If only one application was running when the user presses ALT + TAB, the system sends an event_system_switchend EVENT with Out a corresponding Event_system_switchstart EVENT. |
The user has released ALT + TAB. This event was sent by the system and never by servers. The HWND parameter of the WINEVENTPROC callback function identifies the window to which the user has switched. If only one application are running when the user presses ALT + TAB, the system sends this event without a corresponding E VENT_system_switchstart event. |
Sample code:
//install EVENT Hook void Installeventhook () {G_hwineventhook =:: Setwineventhook (Event_syst
Em_switchstart, Event_system_switchend,//NULL,//Handle to DLL.
S_handlewinevent,//the callback. 0, 0,//Process and thread IDs of interest (0 = all) winevent_outofcontext | winevent_skipownprocess);
Flags.
}//callback function void CALLBACK s_handlewinevent (Hwineventhook hook, DWORD Eventwin, HWND hwnd, Long Idobject, Long Idchild, DWORD Dweventthread, DWORD Dwmsev Enttime) {switch (Eventwin) {case Event_system_switchstart:trace0 ("[EVE Nt_system_menustart] ");
ALT +tab is pressed to break; Case Event_system_switchend:trace0 ("[Event_system_menuend]"); Alt +tab is loosened
Break
} TRACE1 ("Hwnd=0x%.8xn", hwnd); }