fix: Added control for opening emulator js menu on steam deck controller

This commit is contained in:
Simeon Radivoev 2026-03-15 16:42:11 +02:00
parent fe80b074d2
commit f33c928633
Signed by: simeonradivoev
GPG key ID: C16C2132A7660C8E

View file

@ -32,6 +32,7 @@ export interface Shortcut
{ {
label?: string; label?: string;
button: GamePadButtonCode; button: GamePadButtonCode;
heldTime?: number;
action?: (e: GamepadButtonEvent) => void; action?: (e: GamepadButtonEvent) => void;
} }
@ -55,7 +56,7 @@ import.meta.hot?.dispose(() => shortcutMap.clear());
export function useShortcutContext () export function useShortcutContext ()
{ {
const [array, setArray] = useState<Shortcut[] | undefined>(); const [array, setArray] = useState<({ key: string; } & Shortcut)[] | undefined>();
useEffect(() => useEffect(() =>
{ {
@ -65,7 +66,7 @@ export function useShortcutContext ()
const focusKey = getCurrentFocusKey(); const focusKey = getCurrentFocusKey();
const newArray = GetFocusedTree(focusKey) const newArray = GetFocusedTree(focusKey)
.filter(f => shortcutMap.has(f)) .filter(f => shortcutMap.has(f))
.flatMap(f => shortcutMap.get(f)!) .flatMap(f => shortcutMap.get(f)!.map(s => ({ key: f, ...s })))
.filter(s => .filter(s =>
{ {
const empty = !conflictSet.has(s.button); const empty = !conflictSet.has(s.button);
@ -79,6 +80,8 @@ export function useShortcutContext ()
}; };
const shortcuts = new Map(array?.reverse().map(s => [s.button, s]) ?? []); const shortcuts = new Map(array?.reverse().map(s => [s.button, s]) ?? []);
const holdChecks = new Map<GamePadButtonCode, NodeJS.Timeout>();
const handleGamepadButtonDown = (e: Event) => const handleGamepadButtonDown = (e: Event) =>
{ {
const event = e as GamepadButtonEvent; const event = e as GamepadButtonEvent;
@ -90,7 +93,21 @@ export function useShortcutContext ()
if (shortcuts.has(event.button)) if (shortcuts.has(event.button))
{ {
shortcuts.get(event.button)?.action?.(event); const shortcut = shortcuts.get(event.button);
if (shortcut)
{
if (shortcut.heldTime && shortcut.heldTime > 0)
{
holdChecks.set(event.button, setTimeout(() =>
{
shortcut.action?.(event);
}, shortcut.heldTime));
} else
{
shortcut.action?.(event);
}
}
} }
else if (event.button === GamePadButtonCode.A) else if (event.button === GamePadButtonCode.A)
{ {
@ -120,6 +137,14 @@ export function useShortcutContext ()
{ {
dispatchFocusedEvent(new KeyboardEvent('keyup', { key: 'Enter', code: 'Enter', charCode: 13, keyCode: 13, view: window, bubbles: true })); dispatchFocusedEvent(new KeyboardEvent('keyup', { key: 'Enter', code: 'Enter', charCode: 13, keyCode: 13, view: window, bubbles: true }));
} }
if (shortcuts.has(event.button))
{
if (holdChecks.has(event.button))
{
clearInterval(holdChecks.get(event.button));
}
}
}; };
function compareShortcut (a: Shortcut, b: Shortcut) function compareShortcut (a: Shortcut, b: Shortcut)
@ -157,6 +182,7 @@ export function useShortcutContext ()
window.removeEventListener('gamepadbuttonup', handleGamepadButtonUp); window.removeEventListener('gamepadbuttonup', handleGamepadButtonUp);
window.removeEventListener('shortcutsChanged', handleShortcutRebuild); window.removeEventListener('shortcutsChanged', handleShortcutRebuild);
window.removeEventListener('keydown', handleKeyPress); window.removeEventListener('keydown', handleKeyPress);
holdChecks.forEach(c => clearInterval(c));
}; };
}, [array]); }, [array]);