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): stop AltGr from firing a bare ctrl+alt shortcut

rubenluetke10-beep37f890500e6c
diff --git a/jarvis/trigger/backends/global_hotkeys.py b/jarvis/trigger/backends/global_hotkeys.py
index b4ff274cb7..1aea914d92 100644
--- a/jarvis/trigger/backends/global_hotkeys.py
+++ b/jarvis/trigger/backends/global_hotkeys.py
@@ -579,8 +579,66 @@ def _async_key_is_down(vk: int) -> bool | None:
return None
+_VK_RMENU = 0xA5
+# Characters that sit on AltGr on every common AltGr layout (German ``@ € { \``,
+# French ``@ # {``, Nordic ``@ $ {`` …). A US layout types all of them without
+# Ctrl+Alt, so none of them reports that shift state there.
+_ALTGR_PROBE_CHARS = "@€{[]}\|~"
+_altgr_layout_cache: dict[int, bool] = {}
+
+
+def _layout_has_altgr(hkl: int) -> bool:
+ """Does keyboard layout ``hkl`` type any probe character through AltGr?"""
+ cached = _altgr_layout_cache.get(hkl)
+ if cached is not None:
+ return cached
+ import ctypes # noqa: PLC0415 - Windows-only path
+
+ user32 = ctypes.WinDLL("user32", use_last_error=True)
+ user32.VkKeyScanExW.restype = ctypes.c_short
+ user32.VkKeyScanExW.argtypes = (ctypes.c_wchar, ctypes.c_void_p)
+ found = False
+ for ch in _ALTGR_PROBE_CHARS:
+ scan = int(user32.VkKeyScanExW(ch, ctypes.c_void_p(hkl)))
+ # High byte = shift state; 2 = Ctrl, 4 = Alt. -1 = not on this layout.
+ if scan != -1 and (scan >> 8) & 0x06 == 0x06:
+ found = True
+ break
+ _altgr_layout_cache[hkl] = found
+ return found
+
+
+def altgr_is_down() -> bool:
+ """Is AltGr held right now on the foreground window's keyboard layout?
+
+ On an AltGr layout Windows reports the right Alt key as Ctrl+Alt: the
+ driver raises a phantom ``VK_CONTROL`` with ``VK_RMENU``. A modifier-only
+ ``ctrl+alt`` chord then fires on every ``@``/``€`` typed and on the
+ both-Alt appshot gesture. ``False`` whenever it cannot tell (non-Windows,
+ probe failure), so a caller only ever drops an edge on positive evidence.
+ """
+ if _async_key_is_down(_VK_RMENU) is not True:
+ return False
+ try:
+ import ctypes # noqa: PLC0415 - Windows-only path
+
+ user32 = ctypes.WinDLL("user32", use_last_error=True)
+ user32.GetForegroundWindow.restype = ctypes.c_void_p
+ user32.GetWindowThreadProcessId.restype = ctypes.c_ulong
+ user32.GetWindowThreadProcessId.argtypes = (ctypes.c_void_p, ctypes.c_void_p)
+ user32.GetKeyboardLayout.restype = ctypes.c_void_p
+ user32.GetKeyboardLayout.argtypes = (ctypes.c_ulong,)
+ thread_id = user32.GetWindowThreadProcessId(user32.GetForegroundWindow(), None)
+ hkl = user32.GetKeyboardLayout(thread_id) or 0
+ return _layout_has_altgr(int(hkl))
+ except Exception: # noqa: BLE001 - an unknown layout must never drop a hotkey
+ log.debug("AltGr layout probe failed", exc_info=True)
+ return False
+
+
__all__ = [
"MOUSE_BUTTON_TOKENS",
+ "altgr_is_down",
"GlobalHotkeysBackend",
"_KEY_MAP",
"_canonical_token",
diff --git a/jarvis/trigger/hotkey.py b/jarvis/trigger/hotkey.py
index 4c7ec97014..53332268a9 100644
--- a/jarvis/trigger/hotkey.py
+++ b/jarvis/trigger/hotkey.py
@@ -112,6 +112,22 @@ SELF_INPUT_SUPPRESSED_EVENTS: frozenset[str] = frozenset(
}
)
+def _is_bare_ctrl_alt_chord(combo: str) -> bool:
+ """A modifier-only chord holding Ctrl and Alt, e.g. ``ctrl+alt``.
+
+ On an AltGr keyboard layout the right Alt key IS Ctrl+Alt to Windows, so
+ such a chord also fires on every ``@``/``€`` typed and on the both-Alt
+ appshot gesture. An explicit ``right_ctrl`` is a real key, never AltGr.
+ """
+ tokens = [t.strip() for t in _normalize_combo(combo).split("+") if t.strip()]
+ return (
+ "control" in tokens
+ and "alt" in tokens
+ and "right_control" not in tokens
+ and all(t in _MODIFIER_TOKENS for t in tokens)
+ )
+
+
# Tokens that are modifiers, not "real" keys. A hotkey made of ONLY modifiers
# is not a usable trigger; a usable combo needs at least one real key.
_MODIFIER_TOKENS = frozenset(
@@ -705,6 +721,13 @@ class HotkeyTrigger:
return getattr(self._backend, "_gh", None)
def _make_handler(self, event_name: str):
+ # Only START edges are guarded: a dropped release would strand the
+ # latch its press set (see ``SELF_INPUT_SUPPRESSED_EVENTS``).
+ altgr_guarded = event_name in SELF_INPUT_SUPPRESSED_EVENTS and all(
+ _is_bare_ctrl_alt_chord(combo)
+ for combo in self._bindings_cfg.get(event_name.removesuffix("_press")) or ["x"]
+ )
+
def _on_press() -> None:
# Runs on the BACKEND's thread — the Windows poller calls it with
# no try/except around it (``HotkeyChecker.run``), so anything that
@@ -717,6 +740,11 @@ class HotkeyTrigger:
event_name,
)
return
+ if altgr_guarded and _gh_backend.altgr_is_down():
+ log.debug(
+ "Hotkey %r ignored: AltGr, not Ctrl+Alt, is held.", event_name
+ )
+ return
loop = self._loop
if loop is None or loop.is_closed():
return
diff --git a/tests/unit/trigger/test_hotkey_altgr_guard.py b/tests/unit/trigger/test_hotkey_altgr_guard.py
new file mode 100644
index 0000000000..ac5d6a0584
--- /dev/null
+++ b/tests/unit/trigger/test_hotkey_altgr_guard.py
@@ -0,0 +1,69 @@
+"""AltGr must not fire a bare ``ctrl+alt`` shortcut.
+
+On an AltGr keyboard layout (German, French, Nordic, ...) Windows reports the
+right Alt key as Ctrl+Alt. With hold-to-dictate on ``ctrl+alt``, every ``@``
+typed and every both-Alt appshot gesture started a dictation and popped up the
+Jarvis Bar (live log, 2026-09-29).
+"""
+
+from __future__ import annotations
+
+import asyncio
+
+import pytest
+
+from jarvis.trigger.backends import global_hotkeys
+from jarvis.trigger.hotkey import HotkeyTrigger, _is_bare_ctrl_alt_chord
+
+
+def _trigger(combo: str) -> HotkeyTrigger:
+ trig = HotkeyTrigger({"dictate": [combo]}, push_to_talk=frozenset({"dictate"}))
+ trig._loop = asyncio.get_running_loop()
+ return trig
+
+
+async def _fire(trig: HotkeyTrigger, event_name: str) -> list[str]:
+ trig._make_handler(event_name)()
+ await asyncio.sleep(0)
+ out: list[str] = []
+ while not trig._queue.empty():
+ out.append(trig._queue.get_nowait())
+ return out
+
+
+ ("combo", "expected"),
+ [
+ ("ctrl+alt", True),
+ ("ctrl+alt+shift", True),
+ ("ctrl+alt+j", False), # a real key: AltGr+J types nothing
+ ("right_ctrl+alt", False), # the right Ctrl key is never AltGr
+ ("alt", False),
+ ("f1+f2", False),
+ ],
+)
+def test_only_bare_ctrl_alt_chords_are_guarded(combo: str, expected: bool) -> None:
+ assert _is_bare_ctrl_alt_chord(combo) is expected
+
+
+async def test_altgr_drops_the_press_but_never_the_release(monkeypatch) -> None:
+ monkeypatch.setattr(global_hotkeys, "altgr_is_down", lambda: True)
+ trig = _trigger("ctrl+alt")
+ assert await _fire(trig, "dictate_press") == []
+ assert await _fire(trig, "dictate_release") == ["dictate_release"]
+
+
+async def test_real_ctrl_alt_still_fires(monkeypatch) -> None:
+ monkeypatch.setattr(global_hotkeys, "altgr_is_down", lambda: False)
+ assert await _fire(_trigger("ctrl+alt"), "dictate_press") == ["dictate_press"]
+
+
+async def test_a_chord_with_a_real_key_ignores_altgr(monkeypatch) -> None:
+ monkeypatch.setattr(global_hotkeys, "altgr_is_down", lambda: True)
+ assert await _fire(_trigger("ctrl+alt+j"), "dictate_press") == ["dictate_press"]
+
+
+def test_probe_is_a_quiet_no_on_hosts_without_win32() -> None:
+ # Off Windows GetAsyncKeyState is unavailable; the guard must never drop.
+ if global_hotkeys._async_key_is_down(0x10) is None:
+ assert global_hotkeys.altgr_is_down() is False