Another attempt at fixing plugin keyboard shortcuts.

This commit is contained in:
Storm Dragon 2025-04-14 04:54:48 -04:00
parent 0580dda131
commit 0347b7feea
2 changed files with 39 additions and 20 deletions

View File

@ -47,11 +47,9 @@ class APIHelper:
self.app = app self.app = app
self._gestureBindings = {} self._gestureBindings = {}
def registerGestureByString(self, function, name, gestureString, def registerGestureByString(self, function, name, gestureString, inputEventType='default', normalizer='cthulhu', learnModeEnabled=True, contextName=None):
inputEventType='default', normalizer='cthulhu',
learnModeEnabled=True, contextName=None):
"""Register a gesture by string. """Register a gesture by string.
Arguments: Arguments:
- function: the function to call when the gesture is performed - function: the function to call when the gesture is performed
- name: a human-readable name for this gesture - name: a human-readable name for this gesture
@ -60,23 +58,23 @@ class APIHelper:
- normalizer: the normalizer to use - normalizer: the normalizer to use
- learnModeEnabled: whether this should be available in learn mode - learnModeEnabled: whether this should be available in learn mode
- contextName: the context for this gesture (e.g., plugin name) - contextName: the context for this gesture (e.g., plugin name)
Returns the binding ID or None if registration failed Returns the binding ID or None if registration failed
""" """
if not gestureString.startswith("kb:"): if not gestureString.startswith("kb:"):
return None return None
# Extract the key portion from the gesture string # Extract the key portion from the gesture string
key = gestureString.split(":", 1)[1] key = gestureString.split(":", 1)[1]
# Handle Cthulhu modifier specially # Handle Cthulhu modifier specially
if "cthulhu+" in key.lower(): if "cthulhu+" in key.lower():
from . import keybindings from . import keybindings
key_parts = key.lower().split("+") key_parts = key.lower().split("+")
# Determine appropriate modifier mask # Determine appropriate modifier mask
modifiers = keybindings.CTHULHU_MODIFIER_MASK modifiers = keybindings.CTHULHU_MODIFIER_MASK
# Extract the final key (without modifiers) # Extract the final key (without modifiers)
final_key = key_parts[-1] final_key = key_parts[-1]
@ -87,18 +85,23 @@ class APIHelper:
modifiers = keybindings.CTHULHU_CTRL_MODIFIER_MASK modifiers = keybindings.CTHULHU_CTRL_MODIFIER_MASK
elif "alt" in key_parts: elif "alt" in key_parts:
modifiers = keybindings.CTHULHU_ALT_MODIFIER_MASK modifiers = keybindings.CTHULHU_ALT_MODIFIER_MASK
# Create a keybinding handler # Create a keybinding handler
class GestureHandler: class GestureHandler:
def __init__(self, function, description): def __init__(self, function, description):
self.function = function self.function = function
self.description = description self.description = description
def __call__(self, script, inputEvent): def __call__(self, script, inputEvent):
return self.function(script, inputEvent) try:
return function(script, inputEvent)
except Exception as e:
import logging
logging.getLogger(__name__).error(f"Error in keybinding handler: {e}")
return True
handler = GestureHandler(function, name) handler = GestureHandler(function, name)
# Register the binding with the active script # Register the binding with the active script
from . import cthulhu_state from . import cthulhu_state
if cthulhu_state.activeScript: if cthulhu_state.activeScript:
@ -108,20 +111,29 @@ class APIHelper:
keybindings.defaultModifierMask, keybindings.defaultModifierMask,
modifiers, modifiers,
handler) handler)
bindings.add(binding)
# 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: if contextName not in self._gestureBindings:
self._gestureBindings[contextName] = [] self._gestureBindings[contextName] = []
self._gestureBindings[contextName].append(binding) self._gestureBindings[contextName].append(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
return binding return binding
return None return None
def unregisterShortcut(self, binding, contextName=None): def unregisterShortcut(self, binding, contextName=None):
"""Unregister a previously registered shortcut. """Unregister a previously registered shortcut.
Arguments: Arguments:
- binding: the binding to unregister - binding: the binding to unregister
- contextName: the context for this gesture - contextName: the context for this gesture
@ -131,11 +143,18 @@ class APIHelper:
if cthulhu_state.activeScript: if cthulhu_state.activeScript:
bindings = cthulhu_state.activeScript.getKeyBindings() bindings = cthulhu_state.activeScript.getKeyBindings()
bindings.remove(binding) bindings.remove(binding)
# Remove key grab at system level
if hasattr(binding, '_grab_ids'):
for grab_id in binding._grab_ids:
self.app.removeKeyGrab(grab_id)
# Remove from our tracking # Remove from tracking
if contextName in self._gestureBindings: if contextName in self._gestureBindings:
if binding in self._gestureBindings[contextName]: if binding in self._gestureBindings[contextName]:
self._gestureBindings[contextName].remove(binding) self._gestureBindings[contextName].remove(binding)
import gi import gi
import importlib import importlib
import os import os

View File

@ -23,5 +23,5 @@
# Fork of Orca Screen Reader (GNOME) # Fork of Orca Screen Reader (GNOME)
# Original source: https://gitlab.gnome.org/GNOME/orca # Original source: https://gitlab.gnome.org/GNOME/orca
version = "2025.04.11" version = "2025.04.14"
codeName = "testing" codeName = "testing"