[Windows programming] How to intercept Alt + TAB events

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.