Windows 中 Alt + Tab 组合键被用来在各个程序之间切换。 因此,该键盘消 息 (WM_KEYDOWN/UP) 是直接发给系统内核, 在应用程序中的消息循环中截获不 到。
一个常见问题是,可是有的应用程序想在被Alt+TAB 切换到后台之间做点事情 , 这时候该怎么办?
方案之一就是用底层的键盘钩子,截获整个系统的键盘输入。但这样做会导致 一些效率以及稳定性问题。
另外一个比较方便安全的方案就是用 Windows Accessbility API 的 SetWinEventHook 函数, 监听 EVENT_SYSTEM_SWITCHSTART 和 EVENT_SYSTEM_SWITCHEND 事件。
这2个事件就是对应用户按下Alt+Tab键 以及 松开 Alt+Tab键,下面是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. 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. |
EVENT_SYSTEM_SWITCHEND |
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 Hook void InstallEventHook() { g_hWinEventhook = ::SetWinEventHook( EVENT_SYSTEM_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. } // 回调函数 void CALLBACK s_HandleWinEvent(HWINEVENTHOOK hook, DWORD eventWin, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime) { switch (eventWin) { case EVENT_SYSTEM_SWITCHSTART: TRACE0("[EVENT_SYSTEM_MENUSTART] "); // Alt +Tab 被按下 break; case EVENT_SYSTEM_SWITCHEND: TRACE0("[EVENT_SYSTEM_MENUEND] "); // Alt +Tab 被松开 break; } TRACE1("hwnd=0x%.8xn", hwnd); }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索信号截获
, system
, event
, hwnd
, alt
, 编程windowsc++switch应用
, The
, user
tab切换事件
egret截获点击事件、ios 截获点击事件、js截获手机返回事件、ocx 截获 子窗体 事件、alt tab不能切换,以便于您获取更多的相关知识。