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.
This commit is contained in:
Zach Clendenen
2026-07-19 12:06:19 -05:00
parent 549ed33dd7
commit ef07ea5889
3 changed files with 29 additions and 2 deletions

View File

@@ -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) {

View File

@@ -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;

View File

@@ -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<DetailedRom | null>(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);
});
</script>
<template>