mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 10:33:22 +01:00
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:
@@ -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 {
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user