From 235360e5117e100046328ecb44ac00c2aec653b8 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sat, 8 Aug 2026 19:38:04 +0200 Subject: [PATCH] fix(shortcuts): capture rebind keys on window so it works in WebKit --- .../keyboard-shortcuts/KeyboardShortcuts.tsx | 78 ++++++++++--------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/src/app/features/settings/keyboard-shortcuts/KeyboardShortcuts.tsx b/src/app/features/settings/keyboard-shortcuts/KeyboardShortcuts.tsx index dadb6d78b..68a97379a 100644 --- a/src/app/features/settings/keyboard-shortcuts/KeyboardShortcuts.tsx +++ b/src/app/features/settings/keyboard-shortcuts/KeyboardShortcuts.tsx @@ -1,5 +1,4 @@ -import { useState } from 'react'; -import type { KeyboardEvent as ReactKeyboardEvent } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { Box, Button, Scroll, Text, config } from 'folds'; import { PageContent, SettingsSectionPage } from '$components/page'; import { SequenceCard, SequenceCardStyle } from '$components/sequence-card'; @@ -44,7 +43,6 @@ type ShortcutRowProps = { error?: string; onEdit: () => void; onReset: () => void; - onKeyDown: (event: ReactKeyboardEvent) => void; }; function ShortcutRow({ @@ -55,7 +53,6 @@ function ShortcutRow({ error, onEdit, onReset, - onKeyDown, }: ShortcutRowProps) { return ( (); const [error, setError] = useState(); - const updateOverride = (id: ShortcutId, binding: string | null | undefined) => { - setOverrides((current) => { - const next = { ...current }; - if (binding === undefined) delete next[id]; - else next[id] = binding; - return next; - }); - setEditingId(undefined); - setError(undefined); - }; - - const handleCapture = (id: ShortcutId, event: ReactKeyboardEvent) => { - event.preventDefault(); - event.stopPropagation(); - if (event.key === 'Escape') { + const updateOverride = useCallback( + (id: ShortcutId, binding: string | null | undefined) => { + setOverrides((current) => { + const next = { ...current }; + if (binding === undefined) delete next[id]; + else next[id] = binding; + return next; + }); setEditingId(undefined); setError(undefined); - return; - } - if (event.key === 'Backspace' || event.key === 'Delete') { - updateOverride(id, null); - return; - } - const binding = captureShortcut(event); - if (!binding) return; - const conflict = findShortcutConflict(id, binding, overrides); - if (conflict) { - setError(`Already used by “${conflict.label}” in this context.`); - return; - } - updateOverride(id, binding); - }; + }, + [setOverrides] + ); + + useEffect(() => { + const id = editingId; + if (!id) return undefined; + + const handleCapture = (event: KeyboardEvent) => { + event.preventDefault(); + event.stopPropagation(); + if (event.key === 'Escape') { + setEditingId(undefined); + setError(undefined); + return; + } + if (event.key === 'Backspace' || event.key === 'Delete') { + updateOverride(id, null); + return; + } + const binding = captureShortcut(event); + if (!binding) return; + const conflict = findShortcutConflict(id, binding, overrides); + if (conflict) { + setError(`Already used by “${conflict.label}” in this context.`); + return; + } + updateOverride(id, binding); + }; + + window.addEventListener('keydown', handleCapture, true); + return () => window.removeEventListener('keydown', handleCapture, true); + }, [editingId, overrides, updateOverride]); return ( updateOverride(shortcut.id, undefined)} - onKeyDown={(event) => handleCapture(shortcut.id, event)} /> )