diff options
author | Sébastien Dailly <sebastien@dailly.me> | 2025-09-19 08:34:15 +0200 |
---|---|---|
committer | Sébastien Dailly <sebastien@dailly.me> | 2025-09-19 08:34:15 +0200 |
commit | d2571d81e1836c14bf86a76902f81954ec5650c8 (patch) | |
tree | ac34c6dfb745ad4d45ef8fd46c9a07335279f3a1 | |
parent | 1fd24d7406909091192e1efd4d76211bcfa47033 (diff) |
-rw-r--r-- | win32.py | 32 |
1 files changed, 18 insertions, 14 deletions
@@ -82,6 +82,20 @@ class Listener(object): if hwnd is None:
return
+ match_event = event in (win32con.EVENT_OBJECT_FOCUS,
+ win32con.EVENT_OBJECT_NAMECHANGE)
+ if not match_event:
+ return
+
+ # Loop until we found the main window
+ parent = windll.user32.GetParent(hwnd)
+ while parent != 0:
+ hwnd = parent
+ parent = windll.user32.GetParent(hwnd)
+
+ is_foreground = (hwnd == windll.user32.GetForegroundWindow())
+ if not is_foreground:
+ return
length = windll.user32.GetWindowTextLengthW(hwnd)
title = create_unicode_buffer(length + 1)
@@ -92,29 +106,19 @@ class Listener(object): 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:
+ for key in code.keys():
+ print(f"Mapping '{title}' to {key}")
component.handle(Mapping((code, None)))
return
# Get the default mapping to apply if there is any defined
# 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(f"Mapping '{title}' to default")
- component.handle(Mapping(("default", None)))
+ print(f"Mapping '{title}' to default")
+ component.handle(Mapping(("default", None)))
def start(self) -> None:
self.hookIDs = [setHook(self.WinEventProc, et) for et in eventTypes.keys()]
|