1039f55
Pinchord · pinchord.git · Tickets · Changelog
Branches > serial-mods > 1039f55 > files / CONTRIBUTING.md
Horrible press/release override thing
Commit: 1039f553d0c1afed7e5ad506c6c8175759fcaeef latest
Parent: 10aab84
1 changed file:
plover_pinchord_31/extension.py view file
@@ -1,5 +1,6 @@ ## Makes the affix keys work on every entry, even user-defined ones import re+import sys from plover import system from plover.steno import Stroke @@ -41,16 +42,23 @@ else: return with_affixes(prefixes, lookup((stroke_without_affixes.rtfcre,)), suffixes) +engine = None+ class Extension: def __init__(self, engine): self.dictionaries = engine.dictionaries self.original_lookup = self.dictionaries.lookup+ self.engine = engine def start(self): self.dictionaries.lookup = self.lookup+ global engine+ engine = self.engine def stop(self): self.dictionaries.lookup = self.original_lookup+ global engine+ engine = None def lookup(self, outline): # inlines the system name, be aware!@@ -58,3 +66,31 @@ return affixed_lookup(self.original_lookup, Stroke.from_steno(outline[0])) else: return self.original_lookup(outline)++def thread_unsafe_press_or_release(mods, engine, pressed_boolean):+ # Grabs the parse_key_combo function from the engine's keyboard emulation module+ # Replaces it in this call with a version that strips the press or release part+ # send_key_combo calls this function, and presses and releases everything it returns+ # So the press or release part is stripped out+ emulation = engine._keyboard_emulation+ emulation_module = sys.modules[type(emulation).__module__]+ original_parse_key_combo = emulation_module.parse_key_combo+ emulation_module.parse_key_combo = lambda *args: [event for event in original_parse_key_combo(*args)+ if event[1] == pressed_boolean]+ try:+ emulation.send_key_combo(" ".join(mods))+ finally:+ emulation_module.parse_key_combo = original_parse_key_combo++def do_press_or_release(mods, press):+ global engine+ if mods and engine:+ engine._same_thread_hook(thread_unsafe_press_or_release, mods, engine, press)++def press(mods: tuple[str]):+ """Starts pressing down a tuple of mods (like "control", "shift") without releasing them."""+ do_press_or_release(mods, True)++def release(mods):+ """Releases the tuple of mods (like "control", "shift") without pressing them first."""+ do_press_or_release(mods, False)