1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class GlobalEventFilter(QAbstractNativeEventFilter):
def __init__(self): super(GlobalEventFilter, self).__init__()
def nativeEventFilter(self, event_type, sip_voidptr): if event_type == "windows_generic_MSG" or event_type == "windows_dispatcher_MSG": msg = MSG.from_address(sip_voidptr.__init__()) if msg.message in [WM_KEYDOWN, WM_SYSKEYDOWN]: if VK_F4 == msg.wParam and win32api.GetKeyState(VK_MENU) & 0xF000: return True, 1 # True 代表我处理了消息,不需要Qt处理 elif VK_ESCAPE == msg.wParam: return True, 1 # 屏蔽esc键 return False, 1
class App(QApplication):
def __init__(self, *args): super(App, self).__init__(*args)
self.global_event_filter = GlobalEventFilter() self.intallNativeEventFilter(self.global_event_filter)
def exit_app(self): self.removeNativeEventFilter(self.global_event_filter)
|