Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(hotkeys): fire Shift+digit shortcuts on Linux X11

rubenluetke10-beep4dc2e7be3e6a
diff --git a/jarvis/trigger/backends/pynput.py b/jarvis/trigger/backends/pynput.py
index a5378268c6..9de57070ac 100644
--- a/jarvis/trigger/backends/pynput.py
+++ b/jarvis/trigger/backends/pynput.py
@@ -176,6 +176,9 @@ class PynputBackend:
# The live set of canonical tokens currently held down.
self._held: set[str] = set()
self._permission_check = lambda: True
+ # X11 display for unshifting symbol keys; opened on first need, from
+ # the listener thread only. ``False`` = tried and unavailable.
+ self._x_display: object | None | bool = None
def register(self, bindings, on_event=None) -> None:
"""Stash binding rows as token-set combos. Never raises (AD-6).
@@ -229,12 +232,57 @@ class PynputBackend:
# (modifiers, F-keys) expose ``.name``.
char = getattr(key, "char", None)
if char:
+ if not char.isalnum():
+ # X11 reports the SHIFTED symbol: Shift+9 arrives as "(" (US)
+ # or ")" (German), so "ctrl+shift+9" never matched. Bindings
+ # name the key, so a symbol is mapped back to its base key.
+ base = self._unshifted_char(key)
+ if base:
+ return base
return char.lower()
name = getattr(key, "name", None)
if name:
return name.lower()
return None
+ def _unshifted_char(self, key) -> str | None:
+ """The base (level 0) character of an X11 key, or ``None``.
+
+ pynput's X11 ``KeyCode.vk`` is the keysym. The key that carries this
+ keysym on its SHIFT level, read back one level lower, is the key
+ without Shift — layout-correct, because the X server's own keymap does
+ the mapping. A keysym can sit on several keys ("(" is also a keypad
+ key, on its base level); only a shift-level entry counts. Off X11,
+ without Xlib, or for a symbol that is not shifted, ``None`` keeps the
+ character as reported.
+ """
+ vk = getattr(key, "vk", None)
+ if not isinstance(vk, int) or not sys.platform.startswith("linux"):
+ return None
+ display = self._x_display
+ if display is None:
+ try:
+ from Xlib import display as xdisplay # noqa: PLC0415
+
+ display = xdisplay.Display()
+ except Exception: # noqa: BLE001 - no X server or no Xlib: keep the char
+ log.debug("pynput: no X display to unshift symbol keys", exc_info=True)
+ display = False
+ self._x_display = display
+ if display is False:
+ return None
+ try:
+ for keycode, index in display.keysym_to_keycodes(vk): # type: ignore[union-attr]
+ if index % 2 == 0: # an unshifted level: this IS a base key
+ continue
+ base = display.keycode_to_keysym(keycode, index - 1) # type: ignore[union-attr]
+ # Latin-1 keysyms equal their code point; only printable ASCII maps.
+ if 0x20 < base < 0x7F:
+ return chr(base).lower()
+ except Exception: # noqa: BLE001 - an odd keysym keeps its reported char
+ log.debug("pynput: could not unshift keysym %#x", vk, exc_info=True)
+ return None
+
def _on_press_key(self, key) -> None:
token = self._token_for(key)
if token is None:
diff --git a/tests/unit/trigger/test_pynput_shifted_keys.py b/tests/unit/trigger/test_pynput_shifted_keys.py
new file mode 100644
index 0000000000..02fd316f1b
--- /dev/null
+++ b/tests/unit/trigger/test_pynput_shifted_keys.py
@@ -0,0 +1,84 @@
+"""X11 reports Shift+digit as its symbol; the pynput backend maps it back to the key."""
+
+from __future__ import annotations
+
+import sys
+from types import SimpleNamespace
+
+import pytest
+
+from jarvis.trigger.backends.pynput import PynputBackend
+
+#: keysym -> [(keycode, level)] as the X server lists them. "(" also sits on
+#: the keypad's base level (keycode 187), which must not count.
+_US = {0x28: [(187, 0), (18, 1)], 0x21: [(10, 1)]} # "(" on the 9 key, "!" on 1
+_DE = {0x29: [(188, 0), (18, 1)], 0x21: [(10, 1)]} # ")" on the 9 key (German)
+_LEVEL0 = {18: 0x39, 10: 0x31, 187: 0x28, 188: 0x29} # "9", "1", keypad "(" ")"
+
+
+class FakeDisplay:
+ def __init__(self, keysyms: dict[int, list[tuple[int, int]]]) -> None:
+ self.keysyms = keysyms
+
+ def keysym_to_keycodes(self, keysym: int):
+ return iter(self.keysyms.get(keysym, []))
+
+ def keycode_to_keysym(self, keycode: int, index: int) -> int:
+ assert index == 0
+ return _LEVEL0.get(keycode, 0)
+
+
+def _key(char: str) -> SimpleNamespace:
+ return SimpleNamespace(char=char, vk=ord(char))
+
+
+def backend(monkeypatch: pytest.MonkeyPatch):
+ monkeypatch.setattr(sys, "platform", "linux")
+
+ def make(layout: dict[int, int]) -> PynputBackend:
+ instance = PynputBackend()
+ instance._x_display = FakeDisplay(layout) # noqa: SLF001
+ return instance
+
+ return make
+
+
[email protected](("layout", "symbol"), [(_US, "("), (_DE, ")")])
+def test_shift_9_is_the_9_key_on_every_layout(backend, layout, symbol: str) -> None:
+ assert backend(layout)._token_for(_key(symbol)) == "9" # noqa: SLF001
+
+
+def test_ctrl_shift_9_fires_and_releases(backend) -> None:
+ fired: list[str] = []
+ instance = backend(_US)
+ instance.register([("ctrl+shift+9", lambda: fired.append("down"))])
+
+ instance._on_press_key(SimpleNamespace(name="ctrl")) # noqa: SLF001
+ instance._on_press_key(SimpleNamespace(name="shift")) # noqa: SLF001
+ instance._on_press_key(_key("(")) # noqa: SLF001
+ # Shift let go first: the 9 key now reports "9" — the same token, nothing stuck.
+ instance._on_release_key(SimpleNamespace(name="shift")) # noqa: SLF001
+ instance._on_release_key(_key("9")) # noqa: SLF001
+
+ assert fired == ["down"]
+ assert "9" not in instance._held and "(" not in instance._held # noqa: SLF001
+
+
+def test_letters_and_unknown_symbols_keep_their_character(backend) -> None:
+ instance = backend(_US)
+ assert instance._token_for(_key("A")) == "a" # noqa: SLF001
+ assert instance._token_for(_key("€")) == "€" # noqa: SLF001 - no keycode: as reported
+
+
+def test_an_unshifted_symbol_stays_itself(backend) -> None:
+ """A symbol only on a base level (keypad "(" here) is its own key."""
+ instance = backend({0x28: [(187, 0)]})
+ assert instance._token_for(_key("(")) == "(" # noqa: SLF001
+
+
+def test_without_an_x_display_the_character_is_kept(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(sys, "platform", "linux")
+ instance = PynputBackend()
+ instance._x_display = False # noqa: SLF001 - opening the display failed before
+ assert instance._token_for(_key("(")) == "(" # noqa: SLF001