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(trigger): fire toggle hotkeys on the up edge on macOS and Linux

Ruben Lütkea3cc86a7e954
diff --git a/jarvis/trigger/backends/pynput.py b/jarvis/trigger/backends/pynput.py
index e0a183b15a..e7ff5d915a 100644
--- a/jarvis/trigger/backends/pynput.py
+++ b/jarvis/trigger/backends/pynput.py
@@ -15,9 +15,11 @@ form into the pynput key tokens the listener matches against.
A combo is considered *down* when every one of its keys is currently held; the
press handler fires on the transition into that state and the release handler on
-the transition out of it. This is the documented robust both-edges pynput
-pattern (a bare ``Listener`` with manual state), avoiding the press-only
-limitation of ``GlobalHotKeys``.
+the transition out of it. Each edge runs only its OWN handler, so a row carrying
+just one of the two observes just that edge — a toggle binding (``on_release``
+only) fires exactly once on key-up here, on Windows and on macOS alike. This is
+the documented robust both-edges pynput pattern (a bare ``Listener`` with manual
+state), avoiding the press-only limitation of ``GlobalHotKeys``.
macOS permission hint (AD-8 / AD-13)
------------------------------------
@@ -298,14 +300,18 @@ class PynputBackend:
if is_down and not combo["down"]:
combo["down"] = True
self._got_event = True
- handler = combo["on_press"] or combo["on_release"]
- if handler is not None:
- handler()
+ # Each edge fires ONLY its own handler. A toggle binding carries
+ # on_release alone (HotkeyTrigger._build_bindings) so a held key
+ # fires exactly once on the up edge; falling back to it here made
+ # every toggle fire on key-DOWN on this backend while the Windows
+ # path fired on key-up — one config, two behaviours.
+ if combo["on_press"] is not None:
+ combo["on_press"]()
elif not is_down and combo["down"]:
combo["down"] = False
self._got_event = True
- # Only a push-to-talk binding (both edges set) fires on release.
- if combo["on_press"] is not None and combo["on_release"] is not None:
+ # Push-to-talk (both edges set) and toggle (release only) alike.
+ if combo["on_release"] is not None:
combo["on_release"]()
def start(self) -> None:
diff --git a/jarvis/trigger/backends/quartz.py b/jarvis/trigger/backends/quartz.py
index 4b03c580d6..01c9736938 100644
--- a/jarvis/trigger/backends/quartz.py
+++ b/jarvis/trigger/backends/quartz.py
@@ -27,9 +27,10 @@ Known trade-offs (documented, honest):
when a bound combo actually contains one — the tap never asks for the
primary click.
-The combo vocabulary, edge semantics (press fires on the chord-down
-transition, release only for push-to-talk rows), the permission fail-closed
-gate, and ``received_any_event()`` mirror ``PynputBackend`` exactly, so the
+The combo vocabulary, edge semantics (``on_press`` fires on the chord-down
+transition, ``on_release`` on the chord-up transition — each edge only ever
+runs its OWN handler), the permission fail-closed gate, and
+``received_any_event()`` mirror ``PynputBackend`` exactly, so the
``HotkeyTrigger`` call site stays platform-agnostic (AD-7).
"""
@@ -156,14 +157,18 @@ class QuartzHotkeyBackend:
if is_down and not combo["down"]:
combo["down"] = True
self._got_event = True
- handler = combo["on_press"] or combo["on_release"]
- if handler is not None:
- handler()
+ # Each edge fires ONLY its own handler. A toggle binding carries
+ # on_release alone (HotkeyTrigger._build_bindings) so a held key
+ # fires exactly once on the up edge; falling back to it here made
+ # every toggle fire on key-DOWN on this backend while the Windows
+ # path fired on key-up — one config, two behaviours.
+ if combo["on_press"] is not None:
+ combo["on_press"]()
elif not is_down and combo["down"]:
combo["down"] = False
self._got_event = True
- # Only a push-to-talk binding (both edges set) fires on release.
- if combo["on_press"] is not None and combo["on_release"] is not None:
+ # Push-to-talk (both edges set) and toggle (release only) alike.
+ if combo["on_release"] is not None:
combo["on_release"]()
def _handle_key_down(self, keycode: int) -> None:
diff --git a/tests/unit/trigger/test_hotkey_edge_semantics.py b/tests/unit/trigger/test_hotkey_edge_semantics.py
new file mode 100644
index 0000000000..aad684aa35
--- /dev/null
+++ b/tests/unit/trigger/test_hotkey_edge_semantics.py
@@ -0,0 +1,141 @@
+"""Edge semantics every non-Windows hotkey backend must share.
+
+``HotkeyTrigger._build_bindings`` produces ``[combo, on_press, on_release]``
+rows in exactly three shapes:
+
+* **press-only** — ``on_release`` is ``None``.
+* **release-only** — a TOGGLE binding (Call, Hangup, hands-free Dictation). The
+ handler sits on ``on_release`` so a HELD key fires exactly once instead of
+ storming on OS key repeat.
+* **both edges** — push-to-talk: the down edge starts recording, the up edge
+ submits it.
+
+Each backend must run a handler on ITS OWN edge and nowhere else. The pynput and
+Quartz backends used to fall back to ``on_release`` on the chord-DOWN
+transition, so every toggle fired on key-down on macOS and Linux while the
+Windows path fired on key-up — one config, two behaviours. These tests drive the
+chord matcher of both backends through the identical scenarios, so a future edit
+cannot re-open the divergence on one OS alone.
+"""
+
+from __future__ import annotations
+
+import types
+from collections.abc import Callable
+
+import pytest
+
+# The combo under test is ``alt + l`` on both backends.
+_QUARTZ_FLAG_ALT = 1 << 19
+_QUARTZ_KEYCODE_L = 0x25
+
+
+def _pynput_backend():
+ """A ``PynputBackend`` plus chord-down / chord-up drivers for ``alt + l``."""
+ from jarvis.trigger.backends.pynput import PynputBackend
+
+ backend = PynputBackend()
+ # Linux-X11 reports the physical right Alt as ``alt_r``; letters arrive as a
+ # ``KeyCode`` carrying ``.char``.
+ alt = types.SimpleNamespace(char=None, name="alt_r")
+ key_l = types.SimpleNamespace(char="l", name=None)
+
+ def chord_down() -> None:
+ backend._on_press_key(alt)
+ backend._on_press_key(key_l)
+
+ def chord_up() -> None:
+ backend._on_release_key(key_l)
+ backend._on_release_key(alt)
+
+ return backend, chord_down, chord_up
+
+
+def _quartz_backend():
+ """A ``QuartzHotkeyBackend`` plus the same drivers, keyed by keycode+flags."""
+ from jarvis.trigger.backends.quartz import QuartzHotkeyBackend
+
+ backend = QuartzHotkeyBackend()
+ backend._permission_check = lambda: True
+
+ def chord_down() -> None:
+ backend._handle_flags(_QUARTZ_FLAG_ALT)
+ backend._handle_key_down(_QUARTZ_KEYCODE_L)
+
+ def chord_up() -> None:
+ backend._handle_key_up(_QUARTZ_KEYCODE_L)
+ backend._handle_flags(0)
+
+ return backend, chord_down, chord_up
+
+
+_BACKENDS: dict[str, Callable[[], tuple[object, Callable[[], None], Callable[[], None]]]] = {
+ "pynput": _pynput_backend,
+ "quartz": _quartz_backend,
+}
+
+
[email protected](params=sorted(_BACKENDS), ids=sorted(_BACKENDS))
+def chord(request):
+ """Yield ``(backend, chord_down, chord_up)`` for every non-Windows backend."""
+ return _BACKENDS[request.param]()
+
+
+def test_press_only_binding_fires_on_the_down_edge_only(chord) -> None:
+ backend, chord_down, chord_up = chord
+ edges: list[str] = []
+ backend.register([["alt + l", lambda: edges.append("press"), None]])
+
+ chord_down()
+ assert edges == ["press"]
+ chord_up()
+ assert edges == ["press"]
+
+
+def test_release_only_toggle_fires_on_the_up_edge_only(chord) -> None:
+ """A toggle row (``on_press=None``) must NOT fire on the chord-down edge."""
+ backend, chord_down, chord_up = chord
+ edges: list[str] = []
+ backend.register([["alt + l", None, lambda: edges.append("toggle")]])
+
+ chord_down()
+ assert edges == [], "a release-only toggle must stay silent on key-down"
+ chord_up()
+ assert edges == ["toggle"]
+
+
+def test_both_edges_binding_keeps_push_to_talk_working(chord) -> None:
+ backend, chord_down, chord_up = chord
+ edges: list[str] = []
+ backend.register(
+ [["alt + l", lambda: edges.append("down"), lambda: edges.append("up")]]
+ )
+
+ chord_down()
+ assert edges == ["down"]
+ chord_up()
+ assert edges == ["down", "up"]
+
+
+def test_held_toggle_key_fires_exactly_once(chord) -> None:
+ """OS key repeat re-delivers the down edge; the toggle still fires once."""
+ backend, chord_down, chord_up = chord
+ edges: list[str] = []
+ backend.register([["alt + l", None, lambda: edges.append("toggle")]])
+
+ chord_down()
+ chord_down() # key repeat
+ chord_down()
+ assert edges == []
+ chord_up()
+ assert edges == ["toggle"]
+
+
+def test_a_row_without_any_handler_never_raises(chord) -> None:
+ """A degenerate row must be inert on both edges, not an AttributeError."""
+ backend, chord_down, chord_up = chord
+ backend.register([["alt + l", None, None]])
+
+ chord_down()
+ chord_up()
+ assert backend.received_any_event() is True