Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: mirror the keydown guard decision on keyup, and gate movement input the same way (#4748)

[._.]/ Adam Eivy578a9e874836
diff --git a/client/src/components/RapidReader.jsx b/client/src/components/RapidReader.jsx
index 52624f16c4..2f781bbf01 100644
--- a/client/src/components/RapidReader.jsx
+++ b/client/src/components/RapidReader.jsx
@@ -208,7 +208,7 @@ export default function RapidReader({
{onClose && (
<button
type="button"
- onClick={onClose}
+ onClick={onClose} {...noPointerFocusProps}
className="min-h-[44px] min-w-[44px] flex items-center justify-center rounded-lg border border-port-border text-gray-400 hover:text-white hover:bg-port-bg/60 ml-1"
title="Close (Esc)"
aria-label="Close"
@@ -235,7 +235,7 @@ export default function RapidReader({
<div className="flex items-center gap-1 border border-port-border rounded-md overflow-hidden">
<button
type="button"
- onClick={() => setChunkSize(1)}
+ onClick={() => setChunkSize(1)} {...noPointerFocusProps}
className={`px-2 py-1 text-xs ${chunkSize === 1 ? 'bg-port-accent/20 text-port-accent' : 'text-gray-400 hover:text-white'}`}
aria-pressed={chunkSize === 1}
aria-label="Show one word at a time"
@@ -244,7 +244,7 @@ export default function RapidReader({
</button>
<button
type="button"
- onClick={() => setChunkSize(2)}
+ onClick={() => setChunkSize(2)} {...noPointerFocusProps}
className={`px-2 py-1 text-xs ${chunkSize === 2 ? 'bg-port-accent/20 text-port-accent' : 'text-gray-400 hover:text-white'}`}
aria-pressed={chunkSize === 2}
aria-label="Show two words at a time"
diff --git a/client/src/components/meatspace/post/MorseTrainer.jsx b/client/src/components/meatspace/post/MorseTrainer.jsx
index a216e1c2b8..6971ded1d8 100644
--- a/client/src/components/meatspace/post/MorseTrainer.jsx
+++ b/client/src/components/meatspace/post/MorseTrainer.jsx
@@ -10,7 +10,7 @@ import { streakGlyph } from '../../../lib/streakGlyph.js';
import { safeReadJsonStorage, safeWriteStorage } from '../../../lib/safeStorage';
import { resumeAudioContext } from '../../../lib/audioContext.js';
import { MORSE_MATERIAL_MODES, canAdvanceMorseLevel, selectMorsePrompt } from '../../../lib/morsePractice.js';
-import { isPressKey } from '../../../lib/a11yKeyboard.js';
+import { isPressKey, noPointerFocusProps } from '../../../lib/a11yKeyboard.js';
import PostCompletionActions from './PostCompletionActions';
import { startRetryableSaves } from './completionSave';
@@ -858,7 +858,7 @@ export function KeyPad({ keying }) {
<div className="flex items-center justify-between">
<div className="text-xs font-medium text-gray-400 uppercase tracking-wide">Practice Key</div>
<button
- onClick={keying.clear}
+ onClick={keying.clear} {...noPointerFocusProps}
className="flex items-center gap-1 text-[11px] text-gray-500 hover:text-port-error transition-colors"
>
<Eraser size={11} /> Clear
@@ -1371,7 +1371,7 @@ function SendDrill({ keying, prefs, progress, onExit, onContinue, onSessionCompl
<button onClick={decodeNow} className="flex-1 px-4 py-2.5 bg-port-accent hover:bg-port-accent/80 text-white font-medium rounded-lg transition-colors">
Check
</button>
- <button onClick={keying.clear} className="px-4 py-2.5 bg-port-card border border-port-border hover:border-port-accent text-gray-300 rounded-lg transition-colors">
+ <button onClick={keying.clear} {...noPointerFocusProps} className="px-4 py-2.5 bg-port-card border border-port-border hover:border-port-accent text-gray-300 rounded-lg transition-colors">
Clear
</button>
</div>
diff --git a/client/src/hooks/useKeyCapture.js b/client/src/hooks/useKeyCapture.js
index 5b8290275b..6c2808d4bd 100644
--- a/client/src/hooks/useKeyCapture.js
+++ b/client/src/hooks/useKeyCapture.js
@@ -37,6 +37,10 @@ const CLAIM_GUARDS = { ignoreRepeat: false, allowChords: true };
* lives INSIDE a dialog passes `enabledInDialog: true` to opt back in. Both the
* option name and its default mirror `useKeyboardShortcuts`.
*
+ * The guards are evaluated on the KEYDOWN only; the keyup half mirrors that
+ * decision, so a press that was claimed is always released, even if focus or an
+ * open dialog changed while the key was held.
+ *
* Handlers are read through refs, so inline arrows recreated every render (the
* common call-site shape) don't tear down and re-add the listeners; only
* `enabled` flips the subscription. A handler that is absent entirely doesn't
@@ -53,6 +57,9 @@ export default function useKeyCapture({ enabled = true, enabledInDialog = false,
const upRef = useRef(onKeyUp);
downRef.current = onKeyDown;
upRef.current = onKeyUp;
+ // Keys whose keydown the guards turned away, so the matching keyup can make
+ // the same call — see the mirroring note below.
+ const ignoredDownRef = useRef(new Set());
// Presence, not identity — a call site that always passes a handler keeps one
// stable subscription no matter how often its inline arrow is recreated.
@@ -62,20 +69,35 @@ export default function useKeyCapture({ enabled = true, enabledInDialog = false,
useEffect(() => {
if (!enabled) return undefined;
const guards = { ...CLAIM_GUARDS, enabledInDialog };
- const claim = (ref) => (e) => {
- if (shouldIgnoreGlobalKey(e, guards)) return;
+ const ignoredDown = ignoredDownRef.current;
+ // The keyup half MIRRORS whatever the keydown decided rather than
+ // re-evaluating the guards, because the guards read state that can change
+ // mid-press. Native button activation needs the keydown AND the keyup on the
+ // same element, so re-asking on keyup gets it wrong in both directions: Tab
+ // (or a click) onto a button while Space is held would swallow the release
+ // and strand a handler that began a press on the keydown — a Morse keyer
+ // stuck transmitting — while a dialog opening mid-press would do the same.
+ const claim = (ref, isDown) => (e) => {
+ if (isDown) {
+ if (shouldIgnoreGlobalKey(e, guards)) { ignoredDown.add(e.code || e.key); return; }
+ ignoredDown.delete(e.code || e.key);
+ } else if (ignoredDown.delete(e.code || e.key)) {
+ return;
+ }
if (ref.current?.(e) !== true) return;
e.preventDefault();
// Subsumes stopPropagation: also skips the remaining listeners on window.
e.stopImmediatePropagation();
};
- const down = hasDown ? claim(downRef) : null;
- const up = hasUp ? claim(upRef) : null;
+ const down = hasDown ? claim(downRef, true) : null;
+ const up = hasUp ? claim(upRef, false) : null;
if (down) window.addEventListener('keydown', down, true);
if (up) window.addEventListener('keyup', up, true);
return () => {
if (down) window.removeEventListener('keydown', down, true);
if (up) window.removeEventListener('keyup', up, true);
+ // A key held across an unsubscribe has no keyup to clear it.
+ ignoredDown.clear();
};
}, [enabled, enabledInDialog, hasDown, hasUp]);
}
diff --git a/client/src/hooks/useKeyCapture.test.jsx b/client/src/hooks/useKeyCapture.test.jsx
index 28e8a46cfe..732efd9f9e 100644
--- a/client/src/hooks/useKeyCapture.test.jsx
+++ b/client/src/hooks/useKeyCapture.test.jsx
@@ -180,6 +180,40 @@ describe('useKeyCapture', () => {
});
});
+ // A press that was claimed must always be RELEASED, or a handler that began
+ // something on the keydown (the Morse keyer's tone) is stranded. The guards
+ // read state that can change while a key is held — focus moving onto a button,
+ // a dialog opening — so the keyup mirrors the keydown's decision instead of
+ // re-asking.
+ it('releases a claimed press even when focus moves to a button mid-hold', () => {
+ const down = vi.fn(() => true);
+ const up = vi.fn(() => true);
+ const { getByRole } = render(<Probe onKeyDown={down} onKeyUp={up} />);
+
+ fireEvent.keyDown(document.body, { code: 'Space', key: ' ' });
+ expect(down).toHaveBeenCalledTimes(1);
+
+ // Focus lands on a button while Space is still held, so the release arrives
+ // with a button target.
+ fireEvent.keyUp(getByRole('button', { name: 'act' }), { code: 'Space', key: ' ' });
+ expect(up).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not release a press it never claimed', () => {
+ const down = vi.fn(() => true);
+ const up = vi.fn(() => true);
+ const { getByRole } = render(<Probe onKeyDown={down} onKeyUp={up} />);
+ const button = getByRole('button', { name: 'act' });
+
+ // Both halves on the button: the browser owns this press end to end, and
+ // activating a <button> fires on the KEYUP — claiming it would cancel that.
+ fireEvent.keyDown(button, { code: 'Space', key: ' ' });
+ fireEvent.keyUp(button, { code: 'Space', key: ' ' });
+
+ expect(down).not.toHaveBeenCalled();
+ expect(up).not.toHaveBeenCalled();
+ });
+
it('reads the latest handler without re-subscribing', () => {
const first = vi.fn(() => true);
const second = vi.fn(() => true);
diff --git a/client/src/hooks/useKeyboardControls.js b/client/src/hooks/useKeyboardControls.js
index 053899600e..c583ac8d22 100644
--- a/client/src/hooks/useKeyboardControls.js
+++ b/client/src/hooks/useKeyboardControls.js
@@ -1,13 +1,17 @@
import { useRef, useEffect, useCallback } from 'react';
-import { isEditableTarget } from '../lib/a11yKeyboard.js';
+import { shouldIgnoreGlobalKey } from '../lib/a11yKeyboard.js';
// Movement input is only ever meant for the world behind the UI, so a keystroke aimed at
-// a form field or at an open dialog must not reach the rig: typing "w" in the fast-travel
-// search would otherwise walk the avatar, and Space inside the settings drawer would jump
-// it (Tab likewise has to move focus in those contexts, not flip exploration mode).
+// a form field, at a focused HUD button, or at an open dialog must not reach the rig:
+// typing "w" in the fast-travel search would otherwise walk the avatar, Space on a focused
+// HUD button would jump AND press the button, and Space inside the settings drawer would
+// jump it (Tab likewise has to move focus in those contexts, not flip exploration mode).
+// The shared predicate is the same one PlayerController's own Space claim consults, so the
+// two agree on when the rig stands down. Chords and auto-repeat stay allowed through: the
+// rig reads held keys, so dropping either would strand or stutter a movement key.
// Note this is a keyDOWN-only gate — keyup always clears, or a key held when a dialog
// opens would stay stuck down forever.
-const ignoresMovement = (e) => isEditableTarget(e.target) || Boolean(document.querySelector('[aria-modal="true"]'));
+const ignoresMovement = (e) => shouldIgnoreGlobalKey(e, { allowChords: true, ignoreRepeat: false });
export default function useKeyboardControls(onToggleMode) {
const keysRef = useRef(new Set());