From c27035a029cdcbfb854ea0760fd083b5d8870c6d Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Mon, 4 Dec 2023 19:17:06 +0100 Subject: Use of orderedDict in order to give higher priority to custom layer --- macropad.pyw | 4 +++- win32.py | 3 ++- xlib.py | 9 +++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/macropad.pyw b/macropad.pyw index 2687065..55bb1cf 100755 --- a/macropad.pyw +++ b/macropad.pyw @@ -25,8 +25,10 @@ config_file = path.join(script_path, "config.ini") config = configparser.ConfigParser(delimiters="=") config.read(config_file) +from collections import OrderedDict + init_mapping = config["mapping"] -mapping = dict(init_mapping) +mapping = OrderedDict(init_mapping) from queue import Queue q = Queue() diff --git a/win32.py b/win32.py index bff980b..ee146e6 100755 --- a/win32.py +++ b/win32.py @@ -87,10 +87,11 @@ class Listener(object): if title.value is None: return title = str(title.value).lower() - for pattern, code in self.mapping.items(): + for pattern in reversed(self.mapping): if pattern == "default": continue if pattern in title: + code = self.mapping[pattern] self.queue.put ( (code, None) ) return default = self.mapping.get("default", None) diff --git a/xlib.py b/xlib.py index bc2032c..4577e96 100644 --- a/xlib.py +++ b/xlib.py @@ -54,12 +54,17 @@ class Listener(Thread): self.active_window = window_name found = False - for pattern, code in self.mapping.items(): + + # Create a reveverse dictionnary for getting the users added first + # In python, the elements in a dictionnary are sorted by insertion + # order, so we get the user added first here + for pattern in reversed(self.mapping): # Ignore the default layout if pattern == "default": continue - if not (pattern in window_name or pattern in class_name): + if not (pattern.lstrip() in window_name or pattern.lstrip() in class_name): continue + code = self.mapping[pattern] # We found something. At this point, even if we do not update # the layer (because it is the same as the previous) we do not # switch back to the default layer. -- cgit v1.2.3