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(hotkey): let AltGr and right-Ctrl shortcuts actually fire on Windows

Ruben Lütkebebe96de1e65
diff --git a/jarvis/trigger/backends/global_hotkeys.py b/jarvis/trigger/backends/global_hotkeys.py
index 64bf711390..b55696addd 100644
--- a/jarvis/trigger/backends/global_hotkeys.py
+++ b/jarvis/trigger/backends/global_hotkeys.py
@@ -148,6 +148,53 @@ def _normalize_combo(combo: str) -> str:
return " + ".join(_KEY_MAP.get(p, p) for p in parts)
+def _registration_combos(normalized: str) -> list[str]:
+ """Every chord that has to be armed so ``normalized`` can actually fire.
+
+ Windows reports MORE modifier keys as held than the user physically chose,
+ and the library's matcher refuses a chord while any modifier it does not
+ itself name is down (``non_allowed_modifiers`` in ``HotkeyChecker.run``).
+ Two keys are affected, and both produced a shortcut that saved, displayed
+ in the UI, and then never fired once — the "I picked my keys and nothing
+ happens" report:
+
+ * **AltGr.** On a German, French, Nordic, Polish, Brazilian (…) layout the
+ keyboard driver raises ``VK_CONTROL`` alongside ``VK_MENU`` whenever
+ AltGr goes down. A combo the user recorded as ``right_alt+j`` folds to
+ ``alt + j``, which does not name Ctrl — so the phantom Ctrl the driver
+ injected disqualified the chord on every single press. The shipped
+ defaults all happened to spell an explicit ``ctrl``, which is why this
+ never showed up in the shortcuts we ourselves ship.
+ * **The right Ctrl key.** It raises the generic ``VK_CONTROL`` as well as
+ ``VK_RCONTROL``, and ``right_control`` alone names only the specific one.
+
+ They need different repairs, because only one of them is ambiguous:
+
+ * ``right_control`` is a CORRECTION — the generic Ctrl is always up when
+ the right Ctrl is down, so ``control + right_control + …`` is simply the
+ truthful chord. It still fires exclusively on the right-hand key, since
+ the left one never raises ``VK_RCONTROL``.
+ * ``alt`` is a VARIANT. By the time a combo reaches this module, every Alt
+ spelling has folded onto the one token the library knows, so we can no
+ longer tell "the user chose AltGr" from "the user chose the left Alt" —
+ and the two produce different physical key states. So both chords are
+ armed. They are mutually exclusive by construction: the plain one is
+ refused while Ctrl is down, the Ctrl-carrying one requires it, so a press
+ matches exactly one of them and a handler never runs twice.
+
+ Returns the primary chord first; anything after it is a compatibility
+ variant the caller registers in a second pass, so a chord some other action
+ chose EXPLICITLY always wins the registration.
+ """
+ tokens = [p.strip() for p in normalized.split("+") if p.strip()]
+ if "right_control" in tokens and "control" not in tokens:
+ tokens = ["control", *tokens]
+ combos = [" + ".join(tokens)]
+ if "alt" in tokens and "control" not in tokens:
+ combos.append(" + ".join(["control", *tokens]))
+ return combos
+
+
class GlobalHotkeysBackend:
"""Windows ``global-hotkeys`` backend (relocated logic, AD-7).
@@ -186,8 +233,20 @@ class GlobalHotkeysBackend:
# the translated strings for registration AND removal — the two must be
# byte-identical or ``remove_hotkeys`` leaves a stale registration that
# bricks the next re-entry.
- bindings = [[_to_library_combo(row[0]), *row[1:]] for row in bindings]
- combo_strings = [row[0] for row in bindings]
+ #
+ # ``_registration_combos`` may hand back a SECOND chord for the same
+ # binding (the AltGr repair — see its docstring). Those go in their own
+ # list and are registered afterwards, so a chord another action chose
+ # explicitly always claims the registration ahead of a compatibility
+ # variant.
+ primary: list[list] = []
+ compat: list[list] = []
+ for row in bindings:
+ variants = _registration_combos(row[0])
+ primary.append([_to_library_combo(variants[0]), *row[1:]])
+ compat.extend([_to_library_combo(v), *row[1:]] for v in variants[1:])
+ bindings = primary
+ combo_strings = [row[0] for row in primary + compat]
# Idempotent (re-)registration. A previous lifecycle in this process
# may have left these combos in the shared singleton (historically the
@@ -225,6 +284,31 @@ class GlobalHotkeysBackend:
registered_rows.append(row)
registered_strings.append(row[0])
+ # Second pass: the compatibility chords. Skipping one is normal, not a
+ # fault — it means the same chord is already armed, either because
+ # another action named it outright or because two bindings produced the
+ # same variant. The library keys its registry off the token ORDER, so a
+ # hand-written jarvis.toml could spell an existing chord differently and
+ # slip past its duplicate check; comparing the token SETS ourselves is
+ # what keeps that from arming one physical press twice.
+ taken = {frozenset(c.replace(" ", "").split("+")) for c in registered_strings}
+ for row in compat:
+ keys = frozenset(row[0].replace(" ", "").split("+"))
+ if keys in taken:
+ continue
+ try:
+ gh.register_hotkeys([row])
+ except Exception: # noqa: BLE001 — the primary chord is what matters
+ log.debug(
+ "Compatibility chord %r not armed (already registered).",
+ row[0],
+ exc_info=True,
+ )
+ continue
+ taken.add(keys)
+ registered_rows.append(row)
+ registered_strings.append(row[0])
+
if not registered_rows:
# Package present but NOT ONE combo registered — degrade like a hard
# failure so the checker never starts on an empty registration;
diff --git a/tests/unit/trigger/test_hotkey_backends.py b/tests/unit/trigger/test_hotkey_backends.py
index 0b6ad7bbab..3e857484bc 100644
--- a/tests/unit/trigger/test_hotkey_backends.py
+++ b/tests/unit/trigger/test_hotkey_backends.py
@@ -278,6 +278,107 @@ def test_global_backend_all_combos_bad_degrades(fake_gh):
assert fake_gh.start_calls == 0
+# ----------------------------------------------------------------------
+# Windows reports more modifiers than the user pressed (_registration_combos).
+#
+# The matcher refuses a chord while any modifier it does not name is down, and
+# two keys always raise one the user never chose: AltGr injects VK_CONTROL on a
+# German/French/Nordic/... layout, and the right Ctrl raises the generic
+# VK_CONTROL next to VK_RCONTROL. Both used to save, display in the UI and then
+# never fire — the "I picked my own keys and nothing happens" report.
+# ----------------------------------------------------------------------
+
+
+def test_altgr_combo_is_also_armed_with_the_ctrl_the_driver_injects(fake_gh):
+ """REGRESSION: an AltGr shortcut has to fire on a layout that HAS AltGr.
+
+ ``right_alt+j`` folds to ``alt + j``, which the matcher rejects the moment
+ AltGr's phantom Ctrl is down — i.e. on every real press of that very key.
+ Arming the Ctrl-carrying chord as well is what makes the user's choice work.
+ """
+ from jarvis.trigger.backends.global_hotkeys import GlobalHotkeysBackend
+
+ fired: list[str] = []
+ backend = GlobalHotkeysBackend()
+ backend.register([["alt + j", None, lambda: fired.append("dictate")]])
+ backend.start()
+
+ assert "alt+j" in fake_gh.registered # a plain left-Alt press still matches
+ assert "control+alt+j" in fake_gh.registered # and so does AltGr
+
+ fake_gh.fire("control + alt + j")
+ assert fired == ["dictate"]
+ backend.stop()
+
+
+def test_right_ctrl_combo_names_the_generic_ctrl_it_raises(fake_gh):
+ """REGRESSION: the right Ctrl key raises VK_CONTROL too, so name it.
+
+ Unlike AltGr this is not ambiguous — the generic Ctrl is ALWAYS down when
+ the right one is — so the chord is corrected rather than duplicated. It
+ still fires only on the right-hand key: the left one never raises
+ ``right_control``.
+ """
+ from jarvis.trigger.backends.global_hotkeys import GlobalHotkeysBackend
+
+ fired: list[str] = []
+ backend = GlobalHotkeysBackend()
+ backend.register([["right_control + j", None, lambda: fired.append("call")]])
+ backend.start()
+
+ assert "control+right_control+j" in fake_gh.registered
+ assert "right_control+j" not in fake_gh.registered # the dead spelling is gone
+
+ fake_gh.fire("control + right_control + j")
+ assert fired == ["call"]
+ backend.stop()
+
+
+def test_compatibility_chord_never_steals_one_another_action_chose(fake_gh):
+ """An explicitly chosen chord outranks another binding's AltGr variant.
+
+ ``alt+j``'s variant is ``control + alt + j``, which is also exactly what a
+ second action spelled out. Registering it twice is impossible, so the
+ explicit owner keeps it and only its handler runs.
+ """
+ from jarvis.trigger.backends.global_hotkeys import GlobalHotkeysBackend
+
+ fired: list[str] = []
+ backend = GlobalHotkeysBackend()
+ backend.register(
+ [
+ ["alt + j", None, lambda: fired.append("dictate")],
+ ["control + alt + j", None, lambda: fired.append("call")],
+ ]
+ )
+ backend.start()
+ fake_gh.fire("control + alt + j")
+ assert fired == ["call"]
+ backend.stop()
+
+
+def test_compatibility_chord_is_removed_on_teardown(fake_gh):
+ """Both chords must come off, or re-entry dies on "already registered"."""
+ from jarvis.trigger.backends.global_hotkeys import GlobalHotkeysBackend
+
+ backend = GlobalHotkeysBackend()
+ backend.register([["alt + j", None, lambda: None]])
+ backend.start()
+ backend.stop()
+ backend.unregister()
+ assert fake_gh.registered == {}
+
+
+def test_registration_combos_leaves_ordinary_chords_alone():
+ """No variant for a chord Windows reports exactly as pressed."""
+ from jarvis.trigger.backends.global_hotkeys import _registration_combos
+
+ assert _registration_combos("f1 + f2") == ["f1 + f2"]
+ assert _registration_combos("control + alt + j") == ["control + alt + j"]
+ assert _registration_combos("shift + f7") == ["shift + f7"]
+ assert _registration_combos("window + j") == ["window + j"]
+
+
def test_global_backend_missing_package_degrades():
"""No global_hotkeys package → register degrades to a no-op, no raise."""
from jarvis.trigger.backends.global_hotkeys import GlobalHotkeysBackend