From d8df2ed757f0f6ffad6f96100578d4fba28ca4a5 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Sun, 20 Apr 2025 02:43:59 -0400 Subject: [PATCH] Another try at getting keybindings working. --- src/cthulhu/cthulhu.py | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/cthulhu/cthulhu.py b/src/cthulhu/cthulhu.py index e7e2420..af8bbc3 100644 --- a/src/cthulhu/cthulhu.py +++ b/src/cthulhu/cthulhu.py @@ -87,30 +87,32 @@ class APIHelper: handler = GestureHandler(function, name) - # Register the binding with the active script + # Create the binding object regardless of whether there's an active script + # This allows plugins to define bindings that will work when a script becomes active from . import cthulhu_state - if cthulhu_state.activeScript: - bindings = cthulhu_state.activeScript.getKeyBindings() - binding = keybindings.KeyBinding( - final_key, - keybindings.defaultModifierMask, - modifiers, - handler) + from . import keybindings + binding = keybindings.KeyBinding( + final_key, + keybindings.defaultModifierMask, + modifiers, + handler) - # Add the binding to the active script - bindings.add(binding) - - # Store binding for later reference + # Store binding for later reference if contextName not in self._gestureBindings: self._gestureBindings[contextName] = [] - self._gestureBindings[contextName].append(binding) # This line should be outside the if block - - # Register key grab at the system level - grab_ids = self.app.addKeyGrab(binding) - - # For later removal - if grab_ids: - binding._grab_ids = grab_ids + self._gestureBindings[contextName].append(binding) + + # Only add to active script if one exists + if cthulhu_state.activeScript: + bindings = cthulhu_state.activeScript.getKeyBindings() + bindings.add(binding) + + # Register key grab at the system level + grab_ids = self.app.addKeyGrab(binding) + + # For later removal + if grab_ids: + binding._grab_ids = grab_ids logger.info(f"Created binding: {binding.keysymstring} with modifiers {binding.modifiers}") return binding