import { useEffect, useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { Keyboard, X } from 'lucide-react'; import { cn } from '@/lib/utils'; interface KeyboardShortcutsProps { onStep?: () => void; onRestart?: () => void; onAutoPlay?: () => void; onToggleEditor?: () => void; disabled?: boolean; } const SHORTCUTS = [ { key: 'Space', label: 'Step / Start', action: 'step' }, { key: 'R', label: 'Restart episode', action: 'restart' }, { key: 'A', label: 'Toggle auto-play', action: 'autoplay' }, { key: 'E', label: 'Toggle protocol editor', action: 'editor' }, { key: '1', label: 'Easy difficulty', action: 'diff1' }, { key: '2', label: 'Medium difficulty', action: 'diff2' }, { key: '3', label: 'Hard difficulty', action: 'diff3' }, { key: '?', label: 'Show shortcuts', action: 'help' }, { key: 'Esc', label: 'Close overlays', action: 'close' }, ]; export function useKeyboardShortcuts({ onStep, onRestart, onAutoPlay, onToggleEditor, disabled, }: KeyboardShortcutsProps) { const [showHelp, setShowHelp] = useState(false); useEffect(() => { function handler(e: KeyboardEvent) { // Don't trigger when typing in inputs const tag = (e.target as HTMLElement)?.tagName; if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return; if (disabled && e.key !== '?' && e.key !== 'Escape') return; switch (e.key) { case ' ': e.preventDefault(); onStep?.(); break; case 'r': case 'R': onRestart?.(); break; case 'a': case 'A': onAutoPlay?.(); break; case 'e': case 'E': onToggleEditor?.(); break; case '?': setShowHelp((p) => !p); break; case 'Escape': setShowHelp(false); break; } } window.addEventListener('keydown', handler); return () => window.removeEventListener('keydown', handler); }, [onStep, onRestart, onAutoPlay, onToggleEditor, disabled]); return { showHelp, setShowHelp }; } export function ShortcutsOverlay({ show, onClose }: { show: boolean; onClose: () => void }) { return ( {show && ( e.stopPropagation()} >

Keyboard Shortcuts

{SHORTCUTS.map((s) => (
{s.label} {s.key}
))}

Press ? anytime to toggle

)}
); } export function ShortcutHint({ className }: { className?: string }) { return ( Press ? for shortcuts ); }