From ef07ea5889cd981ea7357f5ee767079b9e40123f Mon Sep 17 00:00:00 2001 From: Zach Clendenen Date: Sun, 19 Jul 2026 12:06:19 -0500 Subject: [PATCH] fix(v2): mute global hotkeys while a game owns the keyboard The g-prefix sequences and the / search shortcut listened during play, so pressing common game keys (g then h/p/c, or /) navigated away and killed the session. Gate the handler on the playing store flag. EmulatorJS set the flag on play but never cleared it on unmount, leaving pad and hotkey navigation dead after exiting a game. Ruffle never set it at all, so Flash sessions had no protection. Both players now flag the session on play and release it on unmount. --- .../v2/composables/useGlobalHotkeys/index.ts | 9 ++++++++- frontend/src/v2/views/Player/EmulatorJS.vue | 3 +++ frontend/src/v2/views/Player/Ruffle.vue | 19 ++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/frontend/src/v2/composables/useGlobalHotkeys/index.ts b/frontend/src/v2/composables/useGlobalHotkeys/index.ts index ca9630c82..982b54753 100644 --- a/frontend/src/v2/composables/useGlobalHotkeys/index.ts +++ b/frontend/src/v2/composables/useGlobalHotkeys/index.ts @@ -9,10 +9,12 @@ // g c → collections index // // Two-key sequences (Gmail-style) have a 1.2s idle timeout. Everything is -// guarded against input fields and contenteditable. +// guarded against input fields, contenteditable, and a running game +// (the playing store flag), so hotkeys never fire mid-session. import { onBeforeUnmount } from "vue"; import { useRouter } from "vue-router"; import { ROUTES } from "@/plugins/router"; +import storePlaying from "@/stores/playing"; let installed = false; @@ -35,6 +37,7 @@ export function useGlobalHotkeys() { installed = true; const router = useRouter(); + const playingStore = storePlaying(); let pendingPrefix: string | null = null; let pendingAt = 0; const PREFIX_IDLE_MS = 1200; @@ -42,6 +45,10 @@ export function useGlobalHotkeys() { function onKey(e: KeyboardEvent) { if (e.metaKey || e.ctrlKey || e.altKey) return; if (isEditable(e.target)) return; + // While a game is running the emulator owns the keyboard. "g" and + // "h" are common game keys; without this gate a two-key sequence + // (or "/") would navigate away and kill the session. + if (playingStore.playing) return; const now = performance.now(); if (pendingPrefix && now - pendingAt > PREFIX_IDLE_MS) { diff --git a/frontend/src/v2/views/Player/EmulatorJS.vue b/frontend/src/v2/views/Player/EmulatorJS.vue index d13bdfee4..b389938cf 100644 --- a/frontend/src/v2/views/Player/EmulatorJS.vue +++ b/frontend/src/v2/views/Player/EmulatorJS.vue @@ -473,6 +473,9 @@ onBeforeUnmount(() => { // the user never exited the game to the config screen first. stopActivityHeartbeat(); emitActivityStop(); + // Hand the keyboard and gamepad back to the UI; the flag otherwise + // stays true and pad/hotkey navigation is dead until a reload. + playing.value = false; window.EJS_emulator?.callEvent("exit"); removeIOSFullscreenShim.value?.(); removeIOSFullscreenShim.value = null; diff --git a/frontend/src/v2/views/Player/Ruffle.vue b/frontend/src/v2/views/Player/Ruffle.vue index 0ee8e5031..f8512e5ef 100644 --- a/frontend/src/v2/views/Player/Ruffle.vue +++ b/frontend/src/v2/views/Player/Ruffle.vue @@ -4,11 +4,19 @@ // `src/views/Player/RuffleRS/Base.vue` so playback stays identical; only the // chrome is v2. No shared state with EJS — Flash has its own config. import { RBtn, RCard, RIcon, RSwitch } from "@v2/lib"; -import { computed, nextTick, onMounted, ref, watch } from "vue"; +import { + computed, + nextTick, + onBeforeUnmount, + onMounted, + ref, + watch, +} from "vue"; import { useI18n } from "vue-i18n"; import { useRoute, useRouter } from "vue-router"; import { ROUTES } from "@/plugins/router"; import romApi from "@/services/api/rom"; +import storePlaying from "@/stores/playing"; import storeRoms, { type DetailedRom, type SimpleRom } from "@/stores/roms"; import type { RuffleSourceAPI } from "@/types/ruffle"; import { getDownloadPath } from "@/utils"; @@ -25,6 +33,7 @@ const { t } = useI18n(); const route = useRoute(); const router = useRouter(); const { fullscreenOnPlay } = useFullscreenPref(); +const playingStore = storePlaying(); const rom = ref(null); const gameRunning = ref(false); @@ -106,6 +115,9 @@ const platformLabel = computed( function onPlay() { gameRunning.value = true; + // Flash games are keyboard-driven; flag the session so global hotkeys + // and pad-to-UI translation stay muted while the game owns input. + playingStore.setPlaying(true); nextTick(() => { if (!rom.value) return; @@ -182,6 +194,11 @@ onMounted(async () => { }; document.body.appendChild(script); }); + +onBeforeUnmount(() => { + // Hand the keyboard and gamepad back to the UI on any exit path. + playingStore.setPlaying(false); +});