diff options
author | Sébastien Dailly <sebastien@dailly.me> | 2025-05-04 16:41:29 +0200 |
---|---|---|
committer | Sébastien Dailly <sebastien@chimrod.com> | 2025-05-05 17:36:00 +0200 |
commit | 98963f3f4ff313a0ec180572c55ab478c4b03f56 (patch) | |
tree | 7a1be6c8a8b6f19fdc69701822ace2f543bcb1f4 | |
parent | ce71c3f67baccd3e980975f090e89cf13a5bfa97 (diff) |
Updated the win32 client
-rw-r--r--[-rwxr-xr-x] | win32.py | 22 |
1 files changed, 18 insertions, 4 deletions
@@ -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()]
|