import { useEffect } from 'react' interface Shortcut { key: string cmd?: boolean // meta or ctrl shift?: boolean alt?: boolean handler: (e: KeyboardEvent) => void } export function useKeyboardShortcuts(shortcuts: Shortcut[]) { useEffect(() => { const handler = (e: KeyboardEvent) => { for (const s of shortcuts) { const cmdMatch = s.cmd ? (e.metaKey || e.ctrlKey) : !(e.metaKey || e.ctrlKey) const shiftMatch = s.shift ? e.shiftKey : !e.shiftKey const altMatch = s.alt ? e.altKey : !e.altKey if (e.key.toLowerCase() === s.key.toLowerCase() && cmdMatch && shiftMatch && altMatch) { s.handler(e) return } } } window.addEventListener('keydown', handler) return () => window.removeEventListener('keydown', handler) }, [shortcuts]) }