In Windows, the Alt + TAB keys are used to switch between programs. Therefore, the keyboard message (wm_keydown/UP) is sent directly to the system kernel and cannot be intercepted in the message loop in the application.
A common problem is that some applications want to do something between being switched to the background by Alt + TAB. What should we do at this time?
One solution is to use the underlying keyboard hook to intercept the keyboard input of the entire system. However, this will cause some efficiency and stability problems.
Another convenient and secure solution is to listen to event_system_switchstart and event_system_switchend events using the setwineventhook function of the Windows accessbility API.
The two events indicate that the user presses the Alt + Tab key and releases the Alt + Tab key. The following is an explanation of msdn:
Event_system_switchstart |
The user has pressed Alt + TAB, which activates the switch window. This event is sent by the system, never by servers.HwndParameter ofWineventprocCallback 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. |
Event_system_switchend |
The user has released Alt + TAB. This event is sent by the system, never by servers.HwndParameter ofWineventprocCallback 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. |
Sample Code:
// Install event hook <br/> void installeventhook () <br/> {<br/> g_hwineventhook =: setwineventhook (<br/> event_system_switchstart, event_system_switchend, // null, // handle to DLL. <br/> s_handlewinevent, // The callback. <br/> 0, 0, // process and thread IDs of interest (0 = All) <br/> winevent_outofcontext | winevent_skipownprocess); // flags. </P> <p >}</P> <p> // callback function </P> <p> void callback s_handlewinevent (hwineventhook hook, DWORD eventwin, hwnd, <br/> long idobject, long idchild, <br/> DWORD dweventthread, DWORD dwmseventtime) </P> <p >{</P> <p> switch (eventwin) <br/>{< br/> case event_system_switchstart: <br/> trace0 ("[event_system_menustart]"); // Alt + tab is pressed <br/> break; <br/> case event_system_switchend: <br/> trace0 ("[event_system_menuend]"); // Alt + tab is released <br/> break; <br/>}< br/> trace1 ("hwnd = 0x %. 8x/N ", hwnd); </P> <p >}</P> <p>