refactor(v2): ignore synthetic keys in modality tracking (pr-ready step 3)

useInputModality now skips untrusted keydowns, so useGamepad's synthetic
arrows stay pad input at the source instead of each dispatch site
re-asserting the pad.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-09-22 15:31:32 -04:00
parent 780ceb24f4
commit cfa9a62e52
4 changed files with 12 additions and 4 deletions

View File

@@ -243,8 +243,6 @@ export function useGamepad() {
let rafId = 0;
let everSawPad = false;
// Also runs after every synthetic arrow, which useInputModality reads as a
// key press: "key" modality never trips the grid-nav autofocus.
const onAnyInput = () => setModality("pad");
const onConnect = () => setModality("pad");
window.addEventListener("gamepadconnected", onConnect);
@@ -346,7 +344,6 @@ export function useGamepad() {
} else if (!gameOwnsInput && binding && t >= prev.nextRepeatAt) {
// Only synthetic-key bindings repeat while held.
dispatchKey(binding);
onAnyInput();
prev.nextRepeatAt = t + REPEAT_MS;
}
} else {

View File

@@ -56,4 +56,13 @@ describe("useInputModality", () => {
expect(modality.value).toBe("pad");
});
it("leaves script-dispatched keys to whoever dispatched them", async () => {
const { modality, setModality } = await loadFresh();
setModality("pad");
window.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown" }));
expect(modality.value).toBe("pad");
});
});

View File

@@ -59,6 +59,8 @@ export function useInputModality() {
setModality("touch");
};
const onKey = (e: KeyboardEvent) => {
// useGamepad's synthetic arrows are pad input, not keyboard.
if (!e.isTrusted) return;
// Ignore modifier-only presses and clicks that happen to be keyboard-
// triggered — what we care about is real navigational keys.
if (

View File

@@ -24,8 +24,8 @@ export function usePlayFocus(
// own default action first (Tab moving focus, Enter activating the target).
const { start: claimSoon } = useTimeoutFn(
() => {
if (toValue(running)) return;
if (
!toValue(running) &&
shouldClaimFocusOnModality(
modality.value,
document.activeElement,