aboutsummaryrefslogtreecommitdiff
path: root/win32.py
diff options
context:
space:
mode:
Diffstat (limited to 'win32.py')
-rw-r--r--win32.py32
1 files changed, 18 insertions, 14 deletions
diff --git a/win32.py b/win32.py
index e5e59e7..1bdb9df 100644
--- a/win32.py
+++ b/win32.py
@@ -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()]