| 25 | |
| 26 | useEffect(() => { |
| 27 | const handleKeyDown = (event: KeyboardEvent): void => { |
| 28 | const { key, ctrlKey, shiftKey, altKey, metaKey } = event; |
| 29 | |
| 30 | for (const name in shortcuts) { |
| 31 | const shortcut = shortcuts[name as keyof Shortcuts]; |
| 32 | |
| 33 | if ( |
| 34 | shortcut.key.toLowerCase() === key.toLowerCase() && |
| 35 | (shortcut.ctrlOrMetaKey |
| 36 | ? ctrlKey || metaKey |
| 37 | : (shortcut.ctrlKey === undefined || shortcut.ctrlKey === ctrlKey) && |
| 38 | (shortcut.metaKey === undefined || shortcut.metaKey === metaKey)) && |
| 39 | (shortcut.shiftKey === undefined || shortcut.shiftKey === shiftKey) && |
| 40 | (shortcut.altKey === undefined || shortcut.altKey === altKey) |
| 41 | ) { |
| 42 | shortcutEventEmitter.dispatch(name as keyof Shortcuts); |
| 43 | event.preventDefault(); |
| 44 | event.stopPropagation(); |
| 45 | |
| 46 | shortcut.action(); |
| 47 | |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | window.addEventListener('keydown', handleKeyDown); |
| 54 | |