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(appshots): record a shortcut even when a key release is lost

rubenluetke10-beep3ec85cd0b81f
diff --git a/jarvis/appshot/hotkey.py b/jarvis/appshot/hotkey.py
index 487e890634..3fa3cf0bce 100644
--- a/jarvis/appshot/hotkey.py
+++ b/jarvis/appshot/hotkey.py
@@ -78,6 +78,31 @@ def is_gesture(hotkey: str) -> bool:
return hotkey in GESTURES
+def shortcuts_conflict(left: str, right: str) -> bool:
+ """Whether two appshot shortcuts would fire on the same press.
+
+ A both-keys gesture is not the single token ``alt``: both Alts must not
+ block every other Alt shortcut. Ordinary chords collide when one key set
+ is the other, or sits inside it — ``ctrl+b`` also fires while
+ ``ctrl+shift+b`` is held, so the area picker and the recorder would start
+ together.
+ """
+ if not left or not right:
+ return False
+ if is_gesture(left) or is_gesture(right):
+ return left == right
+ from jarvis.trigger.hotkey import combos_collide # noqa: PLC0415
+
+ return combos_collide(left, right)
+
+
+def _pynput_present() -> bool:
+ """The Windows appshot listener is pynput, not the polled hotkey package."""
+ import importlib.util # noqa: PLC0415
+
+ return importlib.util.find_spec("pynput") is not None
+
+
def configured_hotkeys(block: Any) -> dict[str, str]:
"""Scope → normalized shortcut from an ``[appshot]`` config block."""
return {
@@ -96,6 +121,9 @@ class AppshotShortcut:
self._trigger_task: asyncio.Task[None] | None = None
self._busy = False
self._recording_busy = False
+ # One press that arrives while a toggle is still running. Dropping it
+ # made the next press start a second recording instead of stopping.
+ self._pending: dict[str, str] = {}
not_started = ShortcutStatus(hotkey="", armed=False, detail="Not started yet.")
self._statuses: dict[str, ShortcutStatus] = dict.fromkeys(SCOPE_KEYS, not_started)
self._subscribed = False
@@ -131,6 +159,7 @@ class AppshotShortcut:
hotkeys = configured_hotkeys(config.appshot)
owns = current_instance().owns_ambient_duties
combos: dict[str, str] = {}
+ claimed: list[str] = []
for scope, hotkey in hotkeys.items():
if not hotkey:
self._statuses[scope] = ShortcutStatus("", False, "No shortcut set.")
@@ -140,7 +169,7 @@ class AppshotShortcut:
False,
"The main app owns global shortcuts; this instance does not arm them.",
)
- elif hotkey in list(hotkeys.values())[:list(hotkeys).index(scope)]:
+ elif any(shortcuts_conflict(hotkey, previous) for previous in claimed):
self._statuses[scope] = ShortcutStatus(
hotkey, False, "This shortcut is already used by another AppShot action."
)
@@ -150,6 +179,8 @@ class AppshotShortcut:
self._statuses[scope] = self._check_combo(hotkey)
if self._statuses[scope].armed:
combos[scope] = hotkey
+ if hotkey:
+ claimed.append(hotkey)
if combos:
self._trigger_task = asyncio.get_running_loop().create_task(
self._run_combos(combos), name="appshot-hotkey"
@@ -205,6 +236,7 @@ class AppshotShortcut:
@staticmethod
def _check_combo(hotkey: str) -> ShortcutStatus:
+ from jarvis.platform import detect_platform # noqa: PLC0415
from jarvis.platform.probes import has_hotkey # noqa: PLC0415
from jarvis.trigger.hotkey import validate_hotkey # noqa: PLC0415
@@ -213,7 +245,10 @@ class AppshotShortcut:
return ShortcutStatus(
hotkey=hotkey, armed=False, detail=str(getattr(verdict, "reason", "") or "")
)
- if not has_hotkey():
+ # Voice hotkeys poll ``global_hotkeys`` on Windows. AppShot chords use
+ # pynput, so a machine with only that package can still arm them.
+ ready = has_hotkey() or (detect_platform() == "win32" and _pynput_present())
+ if not ready:
return ShortcutStatus(
hotkey=hotkey,
armed=False,
@@ -297,10 +332,21 @@ class AppshotShortcut:
def _fire(self, scope: str = "window") -> None:
busy_field = "_recording_busy" if scope == "recording" else "_busy"
if getattr(self, busy_field):
+ self._pending[busy_field] = scope
return
+ self._begin_take(scope, busy_field)
+
+ def _begin_take(self, scope: str, busy_field: str) -> None:
setattr(self, busy_field, True)
+
+ def _finished(_task: asyncio.Task[None]) -> None:
+ setattr(self, busy_field, False)
+ pending = self._pending.pop(busy_field, None)
+ if pending is not None:
+ self._begin_take(pending, busy_field)
+
task = asyncio.get_running_loop().create_task(self._take(scope), name="appshot-take")
- task.add_done_callback(lambda _t: setattr(self, busy_field, False))
+ task.add_done_callback(_finished)
async def _take(self, scope: str) -> None:
if scope == "recording":
@@ -375,5 +421,6 @@ __all__ = [
"is_gesture",
"get_shortcut",
"normalize_hotkey",
+ "shortcuts_conflict",
"start_appshot_shortcut",
]
diff --git a/jarvis/appshot/key_events.py b/jarvis/appshot/key_events.py
index 1d1b6bee68..d464fdad5a 100644
--- a/jarvis/appshot/key_events.py
+++ b/jarvis/appshot/key_events.py
@@ -7,21 +7,157 @@ matcher while keeping Windows character tokens stable across modifier changes.
from __future__ import annotations
-from jarvis.trigger.backends.pynput import PynputBackend
+import logging
+import sys
+
+from jarvis.trigger.backends.pynput import PynputBackend, _combo_is_down
+
+log = logging.getLogger(__name__)
_NUMPAD_TOKENS = {
0x6A: "multiply_key", 0x6B: "add_key", 0x6D: "subtract_key",
0x6E: "decimal_key", 0x6F: "divide_key",
}
+# Virtual-key codes for modifier tokens pynput leaves in the held set when a
+# key-up never arrives. A stuck Ctrl made the next plain letter look like
+# Ctrl+letter, so the recording shortcut fired while typing.
+_MODIFIER_VK = {
+ "ctrl": 0x11, "control": 0x11, "ctrl_l": 0xA2, "ctrl_r": 0xA3,
+ "shift": 0x10, "shift_l": 0xA0, "shift_r": 0xA1,
+ "alt": 0x12, "alt_l": 0xA4, "alt_r": 0xA5, "alt_gr": 0xA5,
+ "cmd": 0x5B, "cmd_l": 0x5B, "cmd_r": 0x5C,
+ "win": 0x5B, "window": 0x5B, "super": 0x5B, "meta": 0x5B,
+}
+
+_user32 = None
+
+
+def key_is_down(token: str, vk: int | None = None) -> bool | None:
+ """Whether ``token`` is physically down. ``None`` when this host cannot tell."""
+ code = vk if isinstance(vk, int) else _MODIFIER_VK.get(token)
+ if code is None or sys.platform != "win32":
+ return None
+ global _user32
+ try:
+ if _user32 is None:
+ import ctypes # noqa: PLC0415
+
+ _user32 = ctypes.WinDLL("user32", use_last_error=True)
+ _user32.GetAsyncKeyState.argtypes = [ctypes.c_int]
+ _user32.GetAsyncKeyState.restype = ctypes.c_short
+ return bool(_user32.GetAsyncKeyState(code) & 0x8000)
+ except Exception: # noqa: BLE001 — a failed read must not wedge the listener
+ log.debug("appshot: could not read key state", exc_info=True)
+ return None
+
class AppshotKeyEvents(PynputBackend):
"""One keyboard hook; inherited stop() joins it before any re-registration."""
+ def __init__(self) -> None:
+ super().__init__()
+ # The longest chord that has been fully down during this hold. A
+ # shorter chord inside it (area ``ctrl+b`` under recording
+ # ``ctrl+shift+b``) must not fire on the way up.
+ self._latched: set[int] | None = None
+ self._token_vk: dict[str, int] = {}
+
@property
def ready(self) -> bool:
return self._started and self._listener is not None
+ def _remember(self, key: object, token: str) -> None:
+ vk = getattr(key, "vk", None)
+ if not isinstance(vk, int):
+ vk = getattr(getattr(key, "value", None), "vk", None)
+ if isinstance(vk, int):
+ self._token_vk[token] = vk
+
+ def _drop_stale(self, *, keep: str | None) -> None:
+ """Forget keys the OS says are already up, without firing a toggle.
+
+ The key this event is about is left alone: dropping it here would eat
+ the press or release edge the caller is about to handle.
+ """
+ removed = False
+ for token in list(self._held):
+ if token == keep:
+ continue
+ if key_is_down(token, self._token_vk.get(token)) is False:
+ self._held.discard(token)
+ removed = True
+ if not removed:
+ return
+ for combo in self._combos:
+ if combo["down"] and not _combo_is_down(combo["tokens"], self._held):
+ combo["down"] = False
+ if not self._held:
+ self._latched = None
+
+ def _on_press_key(self, key) -> None:
+ token = self._token_for(key)
+ if token is not None:
+ self._remember(key, token)
+ self._drop_stale(keep=token)
+ super()._on_press_key(key)
+
+ def _on_release_key(self, key) -> None:
+ super()._on_release_key(key)
+ self._drop_stale(keep=None)
+
+ def _reconcile(self) -> None:
+ """Fire only the longest chord that is fully held.
+
+ The shared matcher fires every chord whose keys are down, so a shorter
+ shortcut starts as well the moment the longer one is released one key
+ at a time. Once a longer chord has been the winner, it stays the winner
+ until every key is up.
+ """
+ if not self._permission_check():
+ self._held.clear()
+ self._latched = None
+ for combo in self._combos:
+ combo["down"] = False
+ return
+ down_now = [combo for combo in self._combos if _combo_is_down(combo["tokens"], self._held)]
+ if down_now:
+ longest = max(len(combo["tokens"]) for combo in down_now)
+ winners = {id(combo) for combo in down_now if len(combo["tokens"]) == longest}
+ previous = self._latched or set()
+ prev_len = max(
+ (len(combo["tokens"]) for combo in self._combos if id(combo) in previous),
+ default=0,
+ )
+ if self._latched is None or longest > prev_len:
+ for combo in self._combos:
+ if combo["down"] and id(combo) not in winners:
+ combo["down"] = False
+ self._latched = winners
+ winners = self._latched or set()
+ if not self._held:
+ for combo in self._combos:
+ if not combo["down"]:
+ continue
+ combo["down"] = False
+ if id(combo) in winners and combo["on_release"] is not None:
+ self._got_event = True
+ combo["on_release"]()
+ self._latched = None
+ return
+ for combo in self._combos:
+ is_down = id(combo) in winners and _combo_is_down(combo["tokens"], self._held)
+ if is_down and not combo["down"]:
+ combo["down"] = True
+ self._got_event = True
+ if combo["on_press"] is not None:
+ combo["on_press"]()
+ elif not is_down and combo["down"]:
+ combo["down"] = False
+ self._got_event = True
+ if combo["on_release"] is not None:
+ combo["on_release"]()
+
def _token_for(self, key) -> str | None:
vk = getattr(key, "vk", None)
if vk is None:
diff --git a/jarvis/ui/web/appshot_routes.py b/jarvis/ui/web/appshot_routes.py
index 1b6a35ba76..52a0ca3f6b 100644
--- a/jarvis/ui/web/appshot_routes.py
+++ b/jarvis/ui/web/appshot_routes.py
@@ -140,6 +140,7 @@ async def put_settings(request: Request, patch: SettingsPatch) -> dict[str, Any]
get_shortcut,
is_gesture,
normalize_hotkey,
+ shortcuts_conflict,
)
from jarvis.core.config import load_config # noqa: PLC0415
from jarvis.core.config_writer import ( # noqa: PLC0415
@@ -166,8 +167,13 @@ async def put_settings(request: Request, patch: SettingsPatch) -> dict[str, Any]
window = changes.get("hotkey", current["window"])
region = changes.get("region_hotkey", current["region"])
recording = changes.get("recording_hotkey", current["recording"])
- assigned = [key for key in (window, region, recording) if key]
- if len(assigned) != len(set(assigned)):
+ chosen = [key for key in (window, region, recording) if key]
+ overlaps = any(
+ shortcuts_conflict(key, earlier)
+ for index, key in enumerate(chosen)
+ for earlier in chosen[:index]
+ )
+ if overlaps:
raise HTTPException(
status_code=400,
detail="Each AppShot action needs a different shortcut.",
@@ -190,7 +196,9 @@ async def put_settings(request: Request, patch: SettingsPatch) -> dict[str, Any]
reset_service()
shortcut = get_shortcut()
- if shortcut is not None and any(key in changes for key in ("hotkey", "region_hotkey", "recording_hotkey")):
+ if shortcut is not None and any(
+ key in changes for key in ("hotkey", "region_hotkey", "recording_hotkey")
+ ):
await shortcut.reload()
return await asyncio.to_thread(_settings_payload)
diff --git a/jarvis/ui/web/frontend/src/components/appshot/AppshotRecordingPanel.test.tsx b/jarvis/ui/web/frontend/src/components/appshot/AppshotRecordingPanel.test.tsx
index 157494c7b5..931e2421bc 100644
--- a/jarvis/ui/web/frontend/src/components/appshot/AppshotRecordingPanel.test.tsx
+++ b/jarvis/ui/web/frontend/src/components/appshot/AppshotRecordingPanel.test.tsx
@@ -65,6 +65,21 @@ describe("AppShot recording controls", () => {
expect(screen.queryByRole("button", { name: "Stop and save" })).toBeNull();
});
+ it("shows what to do while the recording shortcut is being captured", async () => {
+ const onShortcut = vi.fn(async () => {});
+ vi.stubGlobal("fetch", vi.fn(async () => response({ phase: "idle", id: "", message: "", capability: ready })));
+ render(<AppshotRecordingPanel settings={{ ...settings, recording_hotkey: "" }} saving={false} onShortcut={onShortcut} />);
+ fireEvent.click(await screen.findByTestId("appshots-recording-hotkey-change"));
+ expect(await screen.findByTestId("appshots-recording-hotkey-status")).toHaveProperty(
+ "textContent",
+ expect.stringMatching(/Hold the keys/),
+ );
+ fireEvent.keyDown(window, { code: "ControlLeft", key: "Control", ctrlKey: true });
+ fireEvent.keyDown(window, { code: "Digit9", key: "9", ctrlKey: true });
+ fireEvent.keyUp(window, { code: "Digit9", key: "9", ctrlKey: false });
+ await waitFor(() => expect(onShortcut).toHaveBeenCalledWith("ctrl+9"));
+ });
+
it("keeps older recordings downloadable after reopening the page", async () => {
vi.stubGlobal("fetch", vi.fn(async () => response({ phase: "idle", id: "", message: "",
capability: ready, recent: [{ id: "older", created_at: 100 }],
diff --git a/jarvis/ui/web/frontend/src/components/appshot/AppshotRecordingPanel.tsx b/jarvis/ui/web/frontend/src/components/appshot/AppshotRecordingPanel.tsx
index 9618d678e4..c548be463e 100644
--- a/jarvis/ui/web/frontend/src/components/appshot/AppshotRecordingPanel.tsx
+++ b/jarvis/ui/web/frontend/src/components/appshot/AppshotRecordingPanel.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useState } from "react";
+import { useCallback, useEffect, useState } from "react";
import { Download, Loader2, Square, Video } from "lucide-react";
import { Button } from "@/components/ui/button";
import { useT } from "@/i18n";
@@ -19,6 +19,10 @@ export function AppshotRecordingPanel({ settings, saving, onShortcut }: {
const [error, setError] = useState("");
const [loadError, setLoadError] = useState("");
const [revision, setRevision] = useState(0);
+ const [shortcutNote, setShortcutNote] = useState<string | null>(null);
+ const onShortcutStatus = useCallback((text: string | null) => {
+ setShortcutNote((current) => (current === text ? current : text));
+ }, []);
useEffect(() => {
const controller = new AbortController();
@@ -82,8 +86,13 @@ export function AppshotRecordingPanel({ settings, saving, onShortcut }: {
<p className="text-sm text-foreground">{t("appshots.recording_shortcut")}</p>
<AppshotShortcutField value={settings.recording_hotkey ?? ""} disabled={saving || Boolean(active)}
isMac={/Mac/i.test(navigator.platform || "")} testId="appshots-recording-hotkey"
- label={t("appshots.recording_shortcut")} onSave={onShortcut} />
+ label={t("appshots.recording_shortcut")} onSave={onShortcut} onStatus={onShortcutStatus} />
</div>
+ {shortcutNote && (
+ <p className="mt-2 text-sm text-muted-foreground" data-testid="appshots-recording-hotkey-status">
+ {shortcutNote}
+ </p>
+ )}
{settings.recording_shortcut?.detail && settings.recording_hotkey && !settings.recording_shortcut.armed && (
<p className="mt-2 text-sm text-muted-foreground">{settings.recording_shortcut.detail}</p>
)}
diff --git a/jarvis/ui/web/frontend/src/hooks/hotkeyCombo.test.ts b/jarvis/ui/web/frontend/src/hooks/hotkeyCombo.test.ts
index 85c7035be7..35e8f29299 100644
--- a/jarvis/ui/web/frontend/src/hooks/hotkeyCombo.test.ts
+++ b/jarvis/ui/web/frontend/src/hooks/hotkeyCombo.test.ts
@@ -92,6 +92,7 @@ describe("codeToModifierToken", () => {
expect(codeToModifierToken("ShiftRight")).toBe("shift");
expect(codeToModifierToken("AltLeft")).toBe("alt");
expect(codeToModifierToken("AltRight")).toBe("right_alt");
+ expect(codeToModifierToken("AltGraph")).toBe("right_alt");
expect(codeToModifierToken("MetaLeft", "pc")).toBe("win");
});
diff --git a/jarvis/ui/web/frontend/src/hooks/useHotkey.ts b/jarvis/ui/web/frontend/src/hooks/useHotkey.ts
index 995c74f03b..c7d471eaa9 100644
--- a/jarvis/ui/web/frontend/src/hooks/useHotkey.ts
+++ b/jarvis/ui/web/frontend/src/hooks/useHotkey.ts
@@ -233,6 +233,7 @@ export function codeToModifierToken(
case "AltLeft":
return "alt";
case "AltRight":
+ case "AltGraph":
return "right_alt";
case "MetaLeft":
case "MetaRight":
diff --git a/jarvis/ui/web/frontend/src/i18n/locales/de.json b/jarvis/ui/web/frontend/src/i18n/locales/de.json
index c58fec6491..2b17715436 100644
--- a/jarvis/ui/web/frontend/src/i18n/locales/de.json
+++ b/jarvis/ui/web/frontend/src/i18n/locales/de.json
@@ -6417,6 +6417,7 @@
"shortcut_recording": "Neue Tasten drücken…",
"shortcut_recording_hint": "Tasten halten, dann loslassen. Beide Alt-, beide Shift- oder beide Strg-Tasten zusammen gehen auch. Esc bricht ab.",
"shortcut_modifier_only": "Eine Sondertaste allein ist kein Kürzel. Nimm einen Buchstaben dazu oder drücke dieselbe Taste auf beiden Seiten der Tastatur.",
+ "shortcut_unmapped": "Diese Taste kann kein Kürzel sein. Nimm einen Buchstaben, eine Zahl oder eine Funktionstaste.",
"shortcut_both_keys_hint": "Drücke beide {0}-Tasten gleichzeitig",
"region_shortcut_both_keys_hint": "Drücke beide {0}-Tasten zusammen und ziehe dann über den Teil des Bildschirms, den du zeigen willst.",
"target_label": "Appshot-Ziel",
diff --git a/jarvis/ui/web/frontend/src/i18n/locales/en.json b/jarvis/ui/web/frontend/src/i18n/locales/en.json
index f7197c4df4..c9e0721b51 100644
--- a/jarvis/ui/web/frontend/src/i18n/locales/en.json
+++ b/jarvis/ui/web/frontend/src/i18n/locales/en.json
@@ -6450,6 +6450,7 @@
"shortcut_recording": "Press the new keys…",
"shortcut_recording_hint": "Hold the keys, then let go. Both Alt, both Shift or both Ctrl keys together work too. Esc cancels.",
"shortcut_modifier_only": "One modifier key alone is not a shortcut. Add a letter, or press the same key on both sides of the keyboard.",
+ "shortcut_unmapped": "That key cannot be a shortcut. Use a letter, number, or function key.",
"shortcut_both_keys_hint": "Press both {0} keys at the same time",
"region_shortcut_both_keys_hint": "Press both {0} keys together, then drag over the part of the screen you want to show.",
"target_label": "Appshot destination",
diff --git a/jarvis/ui/web/frontend/src/i18n/locales/es.json b/jarvis/ui/web/frontend/src/i18n/locales/es.json
index bfeaaadf83..7a51e8f440 100644
--- a/jarvis/ui/web/frontend/src/i18n/locales/es.json
+++ b/jarvis/ui/web/frontend/src/i18n/locales/es.json
@@ -6417,6 +6417,7 @@
"shortcut_recording": "Pulsa las nuevas teclas…",
"shortcut_recording_hint": "Mantén las teclas y suéltalas. También sirven las dos Alt, las dos Mayús o las dos Ctrl a la vez. Esc cancela.",
"shortcut_modifier_only": "Una tecla modificadora sola no es un atajo. Añade una letra o pulsa la misma tecla a ambos lados del teclado.",
+ "shortcut_unmapped": "Esa tecla no puede ser un atajo. Usa una letra, un número o una tecla de función.",
"shortcut_both_keys_hint": "Pulsa las dos teclas {0} a la vez",
"region_shortcut_both_keys_hint": "Pulsa las dos teclas {0} a la vez y arrastra sobre la parte de la pantalla que quieres mostrar.",
"target_label": "Destino del appshot",
diff --git a/jarvis/ui/web/frontend/src/lib/appshotChord.test.ts b/jarvis/ui/web/frontend/src/lib/appshotChord.test.ts
index 488dc775ba..d23acb6ec2 100644
--- a/jarvis/ui/web/frontend/src/lib/appshotChord.test.ts
+++ b/jarvis/ui/web/frontend/src/lib/appshotChord.test.ts
@@ -22,6 +22,14 @@ describe("chordFromCodes", () => {
expect(chordFromCodes(["Escape"])).toEqual({ problem: "empty" });
});
+ it("treats AltGraph as the right Alt key, and names a key it cannot bind", () => {
+ expect(chordFromCodes(["AltLeft", "AltGraph"])).toEqual({ combo: "alt+alt" });
+ expect(chordFromCodes(["BracketLeft"], new Map([["BracketLeft", "\u00fc"]]))).toEqual({
+ combo: "\u00fc",
+ });
+ expect(chordFromCodes(["PrintScreen"])).toEqual({ problem: "unmapped" });
+ });
+
it("uses the typed letter on QWERTZ while preserving modifier sides", () => {
expect(chordFromCodes(["ControlLeft", "KeyY"], new Map([["KeyY", "z"]])))
.toEqual({ combo: "ctrl+z" });
diff --git a/jarvis/ui/web/frontend/src/lib/appshotChord.ts b/jarvis/ui/web/frontend/src/lib/appshotChord.ts
index 1bfe45fd09..c5a18ec785 100644
--- a/jarvis/ui/web/frontend/src/lib/appshotChord.ts
+++ b/jarvis/ui/web/frontend/src/lib/appshotChord.ts
@@ -14,7 +14,9 @@ import {
isModifierToken,
} from "@/hooks/useHotkey";
-export type ChordResult = { combo: string } | { problem: "modifier_only" | "empty" };
+export type ChordResult =
+ | { combo: string }
+ | { problem: "modifier_only" | "empty" | "unmapped" };
const GESTURES: ReadonlyArray<[string, string, string]> = [
["AltLeft", "AltRight", "alt+alt"],
@@ -29,18 +31,34 @@ export function chordFromCodes(
): ChordResult {
const set = new Set(codes);
set.delete("Escape");
+ // Some browsers name the right Alt key AltGraph. The both-Alt gesture and
+ // the right_alt token both look for AltRight.
+ if (set.delete("AltGraph")) set.add("AltRight");
if (set.size === 0) return { problem: "empty" };
+ // Windows AltGr can emit an extra ControlLeft key event. The backend's
+ // both-Alt gesture watches the two Alt keys, so accept that exact sequence
+ // too. Keep Ctrl intact in ordinary shortcuts and other modifier pairs.
+ if (set.size === 3 && set.has("AltLeft") && set.has("AltRight") && set.has("ControlLeft")) {
+ return { combo: "alt+alt" };
+ }
if (set.size === 2) {
for (const [left, right, gesture] of GESTURES) {
if (set.has(left) && set.has(right)) return { combo: gesture };
}
}
const tokens = new Set<string>();
+ let unmapped = false;
for (const code of set) {
const token = codeToModifierToken(code) ?? characterTokens.get(code) ?? codeToKeyToken(code);
if (token) tokens.add(token);
+ else unmapped = true;
+ }
+ // A key this shortcut vocabulary cannot name (Print Screen, a media key)
+ // must not be reported as "you only pressed Shift". That sentence sent
+ // people looking for a modifier they never touched.
+ if (![...tokens].some((token) => !isModifierToken(token))) {
+ return { problem: unmapped ? "unmapped" : "modifier_only" };
}
- if (![...tokens].some((token) => !isModifierToken(token))) return { problem: "modifier_only" };
return { combo: composeCombo(tokens) };
}
diff --git a/jarvis/ui/web/frontend/src/views/AppshotShortcutField.tsx b/jarvis/ui/web/frontend/src/views/AppshotShortcutField.tsx
index 07002e950c..626689b4f9 100644
--- a/jarvis/ui/web/frontend/src/views/AppshotShortcutField.tsx
+++ b/jarvis/ui/web/frontend/src/views/AppshotShortcutField.tsx
@@ -2,6 +2,7 @@ import { Fragment, useCallback, useEffect, useRef, useState } from "react";
import { Loader2, X } from "lucide-react";
import { useT } from "@/i18n";
+import { codeToModifierToken, composeCombo } from "@/hooks/useHotkey";
import { formatAppshotHotkey } from "@/lib/appshotApi";
import { chordFromCodes } from "@/lib/appshotChord";
import { cn } from "@/lib/utils";
@@ -14,12 +15,68 @@ import { cn } from "@/lib/utils";
* of its description instead of pushing the layout around.
*
* Recording listens on `window` in the capture phase, so the keys never reach
- * the rest of the app, and commits when every key is let go — "hold your keys,
- * then let go", like the voice keybinds. Both Alt / both Shift / both Ctrl keys
- * on their own record as the two-sided gestures the backend watches. Esc (on
- * its own), a second click or leaving the window cancels and keeps the old
- * shortcut.
+ * the rest of the app. A finished chord is saved when the keys come up, when
+ * a key-up was swallowed, or when the window loses focus (the Windows key and
+ * Alt do that). A modifier released before the letter stays part of the same
+ * gesture, so the keys do not have to land in the same instant. Both Alt /
+ * both Shift / both Ctrl record as the two-sided gestures the backend watches.
+ * Esc, or a second click before any real key, cancels and keeps the old shortcut.
*/
+
+/** Rescue for a key-up the WebView never delivers. Modifiers do not use it. */
+const LOST_KEYUP_MS = 900;
+
+type FlagWord = { ctrl: boolean; alt: boolean; shift: boolean; meta: boolean };
+type Family = "ctrl" | "alt" | "shift" | "meta";
+
+function flagsOf(event: KeyboardEvent | MouseEvent): FlagWord {
+ return {
+ ctrl: event.ctrlKey,
+ alt: event.altKey,
+ shift: event.shiftKey,
+ meta: event.metaKey,
+ };
+}
+
+function familyOf(code: string): Family | null {
+ if (code.startsWith("Control")) return "ctrl";
+ if (code.startsWith("Alt")) return "alt";
+ if (code.startsWith("Shift")) return "shift";
+ if (code.startsWith("Meta")) return "meta";
+ return null;
+}
+
+function codesFor(family: Family): string[] {
+ switch (family) {
+ case "ctrl":
+ return ["ControlLeft", "ControlRight"];
+ case "alt":
+ return ["AltLeft", "AltRight", "AltGraph"];
+ case "shift":
+ return ["ShiftLeft", "ShiftRight"];
+ case "meta":
+ return ["MetaLeft", "MetaRight"];
+ }
+}
+
+function canonical(family: Family): string {
+ switch (family) {
+ case "ctrl":
+ return "ControlLeft";
+ case "alt":
+ return "AltLeft";
+ case "shift":
+ return "ShiftLeft";
+ case "meta":
+ return "MetaLeft";
+ }
+}
+
+/** The letter the user actually typed, including keys outside A–Z. */
+function typedCharacter(event: KeyboardEvent): string | null {
+ if (event.key.length !== 1 || event.key < " " || event.key === " ") return null;
+ return event.key.toLowerCase();
+}
export function AppshotShortcutField({
value,
isMac,
@@ -40,22 +97,43 @@ export function AppshotShortcutField({
onStatus?: (text: string | null) => void;
}) {
const t = useT();
+ const tRef = useRef(t);
+ tRef.current = t;
+ const valueRef = useRef(value);
+ valueRef.current = value;
+ const isMacRef = useRef(isMac);
+ isMacRef.current = isMac;
const [recording, setRecording] = useState(false);
const [saving, setSaving] = useState(false);
const [problem, setProblem] = useState("");
+ const [draft, setDraft] = useState("");
const onSaveRef = useRef(onSave);
onSaveRef.current = onSave;
const onStatusRef = useRef(onStatus);
onStatusRef.current = onStatus;
+ // The click that ends a recording has to see the keys already pressed.
+ // The listener itself must NOT restart when the page re-renders: `t` is a
+ // new function every render, and the recording panel refreshes every couple
+ // of seconds. Either one used to wipe the gesture between key-down and key-up.
+ const finishGesture = useRef<() => void>(() => {});
useEffect(() => {
- onStatusRef.current?.(recording ? t("appshots.shortcut_recording_hint") : problem || null);
+ const text = problem
+ ? problem
+ : recording
+ ? t("appshots.shortcut_recording_hint")
+ : null;
+ onStatusRef.current?.(text);
}, [recording, problem, t]);
const commit = useCallback(async (hotkey: string) => {
setSaving(true);
try {
await onSaveRef.current(hotkey);
+ setProblem("");
+ } catch (error) {
+ const message = error instanceof Error ? error.message : "";
+ setProblem(message || tRef.current("appshots.save_error").replace("{0}", ""));
} finally {
setSaving(false);
}
@@ -66,55 +144,237 @@ export function AppshotShortcutField({
const pressed = new Set<string>();
const seen = new Set<string>();
const characters = new Map<string, string>();
+ let closed = false;
+ let idle: ReturnType<typeof setTimeout> | undefined;
+ let nativeUnavailable = false;
+ const nativeSeen = new Set<Family>();
- const stop = () => setRecording(false);
- const onKeyDown = (event: KeyboardEvent) => {
+ function preview() {
+ const result = chordFromCodes(seen, characters);
+ if ("combo" in result) {
+ setDraft(result.combo);
+ return;
+ }
+ if (result.problem !== "modifier_only") {
+ setDraft("");
+ return;
+ }
+ const tokens = new Set<string>();
+ for (const code of seen) {
+ const token = codeToModifierToken(code);
+ if (token) tokens.add(token);
+ }
+ setDraft(composeCombo(tokens));
+ }
+
+ function close(save: boolean) {
+ if (closed) return;
+ closed = true;
+ if (idle) clearTimeout(idle);
+ const snapshot = new Set(seen);
+ const typed = new Map(characters);
+ setRecording(false);
+ setDraft("");
+ if (!save) return;
+ const result = chordFromCodes(snapshot, typed);
+ if ("combo" in result) {
+ setProblem("");
+ if (result.combo !== valueRef.current) void commit(result.combo);
+ return;
+ }
+ if (result.problem === "modifier_only") {
+ setProblem(tRef.current("appshots.shortcut_modifier_only"));
+ } else if (result.problem === "unmapped") {
+ setProblem(tRef.current("appshots.shortcut_unmapped"));
+ }
+ }
+
+ // A lone Shift (or Ctrl, then nothing) is not saved, but it also must not
+ // end the gesture: the letter often arrives a moment after the modifier
+ // comes up. Escape and a second click are what cancel.
+ function endIfReleased() {
+ if (closed || pressed.size > 0 || seen.size === 0) return;
+ const result = chordFromCodes(seen, characters);
+ if ("combo" in result) {
+ close(true);
+ return;
+ }
+ if (result.problem === "modifier_only") {
+ setProblem(tRef.current("appshots.shortcut_modifier_only"));
+ return;
+ }
+ if (result.problem === "unmapped") {
+ setProblem(tRef.current("appshots.shortcut_unmapped"));
+ close(false);
+ }
+ }
+
+ function staleKeysOnly() {
+ if (pressed.size === 0) return false;
+ for (const code of pressed) {
+ if (familyOf(code) !== null) return false;
+ }
+ return true;
+ }
+
+ function armRescue() {
+ if (idle) clearTimeout(idle);
+ if (!staleKeysOnly()) return;
+ idle = setTimeout(() => {
+ if (!staleKeysOnly()) return;
+ for (const code of [...pressed]) pressed.delete(code);
+ endIfReleased();
+ }, LOST_KEYUP_MS);
+ }
+
+ function dropReleased(flags: FlagWord, exceptCode?: string) {
+ let changed = false;
+ for (const code of [...pressed]) {
+ if (code === exceptCode) continue;
+ const family = familyOf(code);
+ if (family !== null && !flags[family]) {
+ pressed.delete(code);
+ changed = true;
+ }
+ }
+ return changed;
+ }
+
+ function ensureFlagged(flags: FlagWord) {
+ for (const family of ["ctrl", "alt", "shift", "meta"] as const) {
+ if (!flags[family]) continue;
+ if (codesFor(family).some((code) => pressed.has(code) || seen.has(code))) continue;
+ const code = canonical(family);
+ pressed.add(code);
+ seen.add(code);
+ }
+ }
+
+ function onKeyDown(event: KeyboardEvent) {
event.preventDefault();
event.stopPropagation();
- if (event.repeat) return;
- if (event.code === "Escape" && seen.size === 0) {
- stop();
+ if (event.code === "Escape") {
+ close(false);
+ return;
+ }
+ // Repeats prove the key is still down, so the rescue timer stays away.
+ if (event.repeat) {
+ armRescue();
return;
}
pressed.add(event.code);
seen.add(event.code);
- // Windows/X11 match the logical letter (VK/key symbol), not its US
- // position. On QWERTZ, physical KeyY is the letter Z. macOS's backend
- // deliberately uses physical keycodes, so keep its existing mapping.
- if (!isMac && /^Key[A-Z]$/.test(event.code) && /^[a-z]$/i.test(event.key)) {
- characters.set(event.code, event.key.toLowerCase());
+ // Windows/X11 match the logical letter, not its US position. On a
+ // QWERTZ keyboard physical KeyY is Z. macOS matches physical keycodes,
+ // so a typed letter must not replace the code there.
+ if (!isMacRef.current) {
+ const typed = typedCharacter(event);
+ if (typed) characters.set(event.code, typed);
}
- };
- const onKeyUp = (event: KeyboardEvent) => {
+ const flags = flagsOf(event);
+ ensureFlagged(flags);
+ dropReleased(flags, event.code);
+ setProblem("");
+ preview();
+ armRescue();
+ }
+
+ function onKeyUp(event: KeyboardEvent) {
event.preventDefault();
event.stopPropagation();
pressed.delete(event.code);
- if (pressed.size > 0 || seen.size === 0) return;
- stop();
+ dropReleased(flagsOf(event), event.code);
+ preview();
+ endIfReleased();
+ if (!closed && pressed.size > 0) armRescue();
+ }
+
+ function onMouseMove(event: MouseEvent) {
+ if (!dropReleased(flagsOf(event))) return;
+ preview();
+ endIfReleased();
+ if (!closed && pressed.size > 0) armRescue();
+ }
+
+ function onBlur() {
const result = chordFromCodes(seen, characters);
- if ("combo" in result) {
- setProblem("");
- if (result.combo !== value) void commit(result.combo);
- } else if (result.problem === "modifier_only") {
- setProblem(t("appshots.shortcut_modifier_only"));
- }
+ if ("combo" in result) close(true);
+ else close(false);
+ }
+
+ finishGesture.current = () => {
+ const result = chordFromCodes(seen, characters);
+ close("combo" in result);
};
+ function applyNative(tokens: string[]) {
+ const down = new Set(tokens);
+ const nativeFlags: FlagWord = {
+ ctrl: down.has("ctrl"),
+ alt: down.has("alt"),
+ shift: down.has("shift"),
+ meta: ["cmd", "win", "command", "window", "meta", "super"].some((token) => down.has(token)),
+ };
+ let changed = false;
+ for (const family of ["ctrl", "alt", "shift", "meta"] as const) {
+ if (nativeFlags[family]) nativeSeen.add(family);
+ else if (nativeSeen.has(family)) {
+ nativeSeen.delete(family);
+ for (const code of codesFor(family)) {
+ if (pressed.delete(code)) changed = true;
+ }
+ }
+ }
+ if (!changed) return;
+ preview();
+ endIfReleased();
+ if (!closed && pressed.size > 0) armRescue();
+ }
+
+ async function tickNative() {
+ if (nativeUnavailable || closed) return;
+ try {
+ const response = await fetch("/api/settings/keybinds/held");
+ if (!response.ok || closed) return;
+ const data = (await response.json()) as { available?: boolean; tokens?: string[] };
+ if (data.available !== true) {
+ nativeUnavailable = true;
+ return;
+ }
+ applyNative(Array.isArray(data.tokens) ? data.tokens : []);
+ } catch {
+ // One failed read is not "this computer cannot tell". The next tick retries.
+ }
+ }
+
window.addEventListener("keydown", onKeyDown, true);
window.addEventListener("keyup", onKeyUp, true);
- window.addEventListener("blur", stop);
+ window.addEventListener("mousemove", onMouseMove, true);
+ window.addEventListener("blur", onBlur);
+ const nativeTimer = setInterval(() => void tickNative(), 80);
+ void tickNative();
return () => {
+ closed = true;
+ if (idle) clearTimeout(idle);
+ clearInterval(nativeTimer);
+ finishGesture.current = () => {};
window.removeEventListener("keydown", onKeyDown, true);
window.removeEventListener("keyup", onKeyUp, true);
- window.removeEventListener("blur", stop);
+ window.removeEventListener("mousemove", onMouseMove, true);
+ window.removeEventListener("blur", onBlur);
};
- }, [recording, value, commit, t, isMac]);
+ }, [recording, commit]);
- const keys = value ? formatAppshotHotkey(value, isMac).split(" + ") : [];
+ const shown = recording && draft ? draft : value;
+ const keys = shown ? formatAppshotHotkey(shown, isMac).split(" + ") : [];
const busy = disabled || saving;
return (
- <div className={cn("relative", className)} data-testid={testId}>
+ <div
+ className={cn("relative", className)}
+ data-testid={testId}
+ data-keybind-recording={recording ? "true" : undefined}
+ >
<button
type="button"
disabled={busy}
@@ -122,8 +382,13 @@ export function AppshotShortcutField({
aria-pressed={recording}
title={recording ? undefined : t("appshots.shortcut_change")}
onClick={() => {
+ if (recording) {
+ finishGesture.current();
+ return;
+ }
setProblem("");
- setRecording((on) => !on);
+ setDraft("");
+ setRecording(true);
}}
data-testid={`${testId}-change`}
className={cn(
@@ -136,7 +401,7 @@ export function AppshotShortcutField({
>
{saving ? (
<Loader2 className="h-4 w-4 animate-spin text-muted-foreground" aria-hidden />
- ) : recording ? (
+ ) : recording && !draft ? (
<span className="truncate text-muted-foreground">{t("appshots.shortcut_recording")}</span>
) : keys.length > 0 ? (
keys.map((key, i) => (
@@ -164,7 +429,7 @@ export function AppshotShortcutField({
<X className="h-3.5 w-3.5" aria-hidden />
</button>
)}
- {problem && !recording && (
+ {problem && (
<span className="sr-only" role="alert">
{problem}
</span>
diff --git a/jarvis/ui/web/frontend/src/views/AppshotsView.test.tsx b/jarvis/ui/web/frontend/src/views/AppshotsView.test.tsx
index 67373ecc03..5fffbe0f33 100644
--- a/jarvis/ui/web/frontend/src/views/AppshotsView.test.tsx
+++ b/jarvis/ui/web/frontend/src/views/AppshotsView.test.tsx
@@ -228,6 +228,86 @@ describe("AppshotsView shortcut recorder", () => {
fireEvent.click(await screen.findByTestId("appshots-region-hotkey-clear"));
await waitFor(() => expect(puts()).toEqual([{ region_hotkey: "" }]));
});
+
+ it("keeps a modifier that was released before the letter", async () => {
+ render(<AppshotsView />);
+ fireEvent.click(await screen.findByTestId("appshots-hotkey-change"));
+ fireEvent.keyDown(window, { code: "ControlLeft", key: "Control", ctrlKey: true });
+ fireEvent.keyUp(window, { code: "ControlLeft", key: "Control", ctrlKey: false });
+ expect(puts()).toEqual([]);
+ fireEvent.keyDown(window, { code: "KeyS", key: "s", ctrlKey: false });
+ fireEvent.keyUp(window, { code: "KeyS", key: "s", ctrlKey: false });
+ await waitFor(() => expect(puts()).toEqual([{ hotkey: "ctrl+s" }]));
+ });
+
+ it("saves when a modifier key-up never arrives", async () => {
+ render(<AppshotsView />);
+ fireEvent.click(await screen.findByTestId("appshots-hotkey-change"));
+ fireEvent.keyDown(window, { code: "ControlLeft", key: "Control", ctrlKey: true });
+ fireEvent.keyDown(window, { code: "KeyJ", key: "j", ctrlKey: true });
+ fireEvent.keyUp(window, { code: "KeyJ", key: "j", ctrlKey: false });
+ await waitFor(() => expect(puts()).toEqual([{ hotkey: "ctrl+j" }]));
+ });
+
+ it("saves a function key whose key-up never arrives", async () => {
+ render(<AppshotsView />);
+ const change = await screen.findByTestId("appshots-hotkey-change");
+ vi.useFakeTimers();
+ try {
+ fireEvent.click(change);
+ fireEvent.keyDown(window, { code: "F9", key: "F9" });
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(1000);
+ });
+ } finally {
+ vi.useRealTimers();
+ }
+ await waitFor(() => expect(puts()).toEqual([{ hotkey: "f9" }]));
+ });
+
+ it("saves the chord when the window loses focus", async () => {
+ render(<AppshotsView />);
+ fireEvent.click(await screen.findByTestId("appshots-hotkey-change"));
+ fireEvent.keyDown(window, { code: "ControlLeft", key: "Control", ctrlKey: true });
+ fireEvent.keyDown(window, { code: "KeyS", key: "s", ctrlKey: true });
+ fireEvent.blur(window);
+ await waitFor(() => expect(puts()).toEqual([{ hotkey: "ctrl+s" }]));
+ });
+
+ it("saves keys that were already down when the page renders again", async () => {
+ const view = render(<AppshotsView />);
+ fireEvent.click(await screen.findByTestId("appshots-hotkey-change"));
+ fireEvent.keyDown(window, { code: "ControlLeft", key: "Control", ctrlKey: true });
+ view.rerender(<AppshotsView />);
+ fireEvent.keyDown(window, { code: "KeyS", key: "s", ctrlKey: true });
+ fireEvent.keyUp(window, { code: "KeyS", key: "s", ctrlKey: false });
+ await waitFor(() => expect(puts()).toEqual([{ hotkey: "ctrl+s" }]));
+ });
+
+ it("saves the typed character for a key outside A to Z", async () => {
+ render(<AppshotsView />);
+ fireEvent.click(await screen.findByTestId("appshots-hotkey-change"));
+ fireEvent.keyDown(window, { code: "BracketLeft", key: "\u00fc" });
+ fireEvent.keyUp(window, { code: "BracketLeft", key: "\u00fc" });
+ await waitFor(() => expect(puts()).toEqual([{ hotkey: "\u00fc" }]));
+ });
+
+ it("tells the rest of the app that a shortcut is being recorded", async () => {
+ render(<AppshotsView />);
+ fireEvent.click(await screen.findByTestId("appshots-hotkey-change"));
+ expect(screen.getByTestId("appshots-hotkey").getAttribute("data-keybind-recording")).toBe(
+ "true",
+ );
+ });
+
+ it("a second click saves the keys already held", async () => {
+ render(<AppshotsView />);
+ const change = await screen.findByTestId("appshots-hotkey-change");
+ fireEvent.click(change);
+ fireEvent.keyDown(window, { code: "KeyK", key: "k" });
+ fireEvent.click(change);
+ await waitFor(() => expect(puts()).toEqual([{ hotkey: "k" }]));
+ });
});
describe("AppshotsView editor", () => {
diff --git a/tests/unit/appshot/test_appshot_key_events.py b/tests/unit/appshot/test_appshot_key_events.py
index a9ce963f7e..f59aa1acd9 100644
--- a/tests/unit/appshot/test_appshot_key_events.py
+++ b/tests/unit/appshot/test_appshot_key_events.py
@@ -20,7 +20,10 @@ def test_a_press_and_release_without_a_poll_interval_fires_once():
assert events == ["recording"]
-def test_control_characters_and_release_order_keep_the_same_letter():
+def test_control_characters_and_release_order_keep_the_same_letter(monkeypatch):
+ # The test presses keys the real keyboard does not hold. Trusting the OS
+ # here would drop the simulated Ctrl before Z arrives.
+ monkeypatch.setattr("jarvis.appshot.key_events.key_is_down", lambda *_args: None)
events = []
listener = AppshotKeyEvents()
listener.register([["control + z", None, lambda: events.append("recording")]])
@@ -33,6 +36,41 @@ def test_control_characters_and_release_order_keep_the_same_letter():
assert not listener._held
+def test_a_shorter_chord_does_not_fire_with_the_longer_one(monkeypatch):
+ monkeypatch.setattr("jarvis.appshot.key_events.key_is_down", lambda *_args: None)
+ events = []
+ listener = AppshotKeyEvents()
+ listener.register([
+ ["control + z", None, lambda: events.append("long")],
+ ["z", None, lambda: events.append("short")],
+ ])
+ ctrl = SimpleNamespace(name="ctrl_l", char=None)
+ key = SimpleNamespace(vk=0x5A, char="z")
+ listener._on_press_key(ctrl)
+ listener._on_press_key(key)
+ listener._on_release_key(ctrl)
+ listener._on_release_key(key)
+ assert events == ["long"]
+
+
+def test_a_stuck_modifier_does_not_turn_the_next_letter_into_the_chord(monkeypatch):
+ # The OS says every leftover key is up. Z itself is the key this event
+ # is about, so the sweep must leave it for the press and release edges.
+ monkeypatch.setattr(
+ "jarvis.appshot.key_events.key_is_down",
+ lambda name, _vk=None: False if name != "z" else None,
+ )
+ events = []
+ listener = AppshotKeyEvents()
+ listener.register([["control + z", None, lambda: events.append("go")]])
+ listener._held.add("ctrl_l")
+ key = SimpleNamespace(vk=0x5A, char="z")
+ listener._on_press_key(key)
+ listener._on_release_key(key)
+ assert events == []
+ assert "ctrl_l" not in listener._held
+
+
def test_numpad_keys_keep_the_shared_binding_vocabulary():
listener = AppshotKeyEvents()
assert listener._token_for(SimpleNamespace(vk=0x63, char="3")) == "numpad_3"
@@ -65,9 +103,7 @@ async def test_cancellation_stops_and_unregisters_the_event_listener(monkeypatch
monkeypatch.setattr(key_events, "AppshotKeyEvents", Listener)
owner = AppshotShortcut(object())
- task = asyncio.create_task(owner._run_combos({"recording": "ctrl+z"}))
- # Test this Windows-only dispatch without touching a real hook.
- monkeypatch.setattr("jarvis.appshot.hotkey.sys.platform", "win32")
+ task = asyncio.create_task(owner._run_key_events({"recording": "ctrl+z"}))
await asyncio.wait_for(started.wait(), 1)
task.cancel()
result = await asyncio.gather(task, return_exceptions=True)
@@ -75,6 +111,30 @@ async def test_cancellation_stops_and_unregisters_the_event_listener(monkeypatch
assert calls == ["register", "start", "stop", "unregister"]
+async def test_a_press_during_a_recording_toggle_runs_once_it_finishes():
+ started = asyncio.Event()
+ release = asyncio.Event()
+ calls: list[str] = []
+ owner = AppshotShortcut(object())
+
+ async def take(scope: str) -> None:
+ calls.append(scope)
+ started.set()
+ await release.wait()
+
+ owner._take = take # type: ignore[method-assign]
+ owner._fire("recording")
+ await asyncio.wait_for(started.wait(), 1)
+ owner._fire("recording")
+ assert calls == ["recording"]
+ release.set()
+ for _ in range(5):
+ await asyncio.sleep(0)
+ if len(calls) == 2:
+ break
+ assert calls == ["recording", "recording"]
+
+
async def test_missing_listener_is_reported_as_unarmed(monkeypatch):
from jarvis.appshot import key_events
diff --git a/tests/unit/appshot/test_appshot_region.py b/tests/unit/appshot/test_appshot_region.py
index f9f8933343..38a47d289a 100644
--- a/tests/unit/appshot/test_appshot_region.py
+++ b/tests/unit/appshot/test_appshot_region.py
@@ -265,7 +265,9 @@ class _Instance:
owns_ambient_duties = True
-async def _reload_with(monkeypatch, hotkey: str, region_hotkey: str) -> AppshotShortcut:
+async def _reload_with(
+ monkeypatch, hotkey: str, region_hotkey: str, recording_hotkey: str = "",
+) -> AppshotShortcut:
import jarvis.appshot.hotkey as hotkey_module
import jarvis.core.config as config_module
import jarvis.core.instance as instance_module
@@ -277,6 +279,7 @@ async def _reload_with(monkeypatch, hotkey: str, region_hotkey: str) -> AppshotS
Cfg.appshot.hotkey = hotkey
Cfg.appshot.region_hotkey = region_hotkey
+ Cfg.appshot.recording_hotkey = recording_hotkey
monkeypatch.setattr(config_module, "load_config", lambda: Cfg)
monkeypatch.setattr(instance_module, "current_instance", lambda: _Instance())
monkeypatch.setattr(probes, "has_hotkey", lambda: True)
@@ -312,6 +315,15 @@ async def test_the_same_key_for_both_arms_only_the_window(monkeypatch) -> None:
assert shortcut.armed_combos == [{"window": "ctrl+alt+a"}]
+async def test_a_shorter_chord_blocks_the_later_action(monkeypatch) -> None:
+ shortcut = await _reload_with(monkeypatch, "ctrl+b", "", "ctrl+shift+b")
+
+ assert shortcut.status_for("window").armed
+ assert not shortcut.status_for("recording").armed
+ assert "another AppShot action" in shortcut.status_for("recording").detail
+ assert shortcut.armed_combos == [{"window": "ctrl+b"}]
+
+
async def test_an_empty_area_shortcut_is_simply_off(monkeypatch) -> None:
shortcut = await _reload_with(monkeypatch, "ctrl+alt+a", "")
@@ -391,6 +403,28 @@ def test_one_key_for_both_shortcuts_is_refused_before_writing(client, monkeypatc
assert writes == []
+def test_a_shortcut_inside_another_is_refused_before_writing(client, monkeypatch) -> None:
+ import jarvis.core.config as config_module
+ import jarvis.core.config_writer as writer
+
+ class Cfg:
+ class appshot: # noqa: N801
+ hotkey = "ctrl+b"
+ region_hotkey = ""
+ recording_hotkey = ""
+
+ writes: list = []
+ monkeypatch.setattr(config_module, "load_config", lambda: Cfg)
+ monkeypatch.setattr(writer, "set_appshot_settings", lambda values: writes.append(values))
+
+ longer = client.put("/api/appshot/settings", json={"recording_hotkey": "ctrl+shift+b"})
+ same_keys = client.put("/api/appshot/settings", json={"recording_hotkey": "ctrl+right_alt+b"})
+
+ assert longer.status_code == 400
+ assert same_keys.status_code == 400
+ assert writes == []
+
+
# ----------------------------------------------------------- picker outcomes