From 98963f3f4ff313a0ec180572c55ab478c4b03f56 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sun, 4 May 2025 16:41:29 +0200 Subject: Updated the win32 client --- win32.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) mode change 100755 => 100644 win32.py diff --git a/win32.py b/win32.py old mode 100755 new mode 100644 index 44cee5c..5220cde --- a/win32.py +++ b/win32.py @@ -76,6 +76,7 @@ class Listener(object): else: return None + def callback(self, hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime) -> None: @@ -90,17 +91,30 @@ class Listener(object): title = str(title.value).lower() if title == "": return + + foreground_hwnd = windll.user32.GetForegroundWindow() + if event != win32con.EVENT_OBJECT_FOCUS and foreground_hwnd != hwnd: + return + for pattern, code in self.mapping.items(): if pattern == "default": continue if pattern in title: self.queue.put ( (code, None) ) return - print("Matching '%s' to default" % title) # Get the default mapping to apply if there is any defined - default = self.mapping.get("default", None) - if default is not None: - self.queue.put ( (default, None) ) + # This only applies when the window raising the event is the main + # window + if hwnd == foreground_hwnd: + # We are not allowed to display message into the handler, because + # the GIL is released in the callback. We can’t modifiy elements + # from other threads, as it’s create weird errors : + # + # Fatal Python error: PyEval_RestoreThread: the function must be + # called with the GIL held, but the GIL is released (the current + # Python thread state is NULL) + print("Mapping '%s' to default" % title) + self.queue.put ( ("default", None) ) def start(self) -> None: self.hookIDs = [setHook(self.WinEventProc, et) for et in eventTypes.keys()] -- cgit v1.2.3