[Windows programming] how to intercept ALT + TAB events

Source: Internet
Author: User

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); }

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.