|
| 1 | +import { useEffect, useRef } from "react"; |
| 2 | + |
| 3 | +// Credit: Based on https://github.com/uidotdev/usehooks/blob/experimental/index.js |
| 4 | +// Enhanced for keyboard shortcuts with key combinations and input field detection |
| 5 | + |
| 6 | +interface UseKeyPressOptions { |
| 7 | + event?: "keydown" | "keyup"; |
| 8 | + target?: Window | Document | HTMLElement; |
| 9 | + eventOptions?: boolean | AddEventListenerOptions; |
| 10 | +} |
| 11 | + |
| 12 | +// Helper function to parse key combinations like "Meta+Enter" or "Control+Enter" |
| 13 | +function parseKeySpec(keySpec: string): { |
| 14 | + key: string; |
| 15 | + ctrlKey?: boolean; |
| 16 | + metaKey?: boolean; |
| 17 | + shiftKey?: boolean; |
| 18 | + altKey?: boolean; |
| 19 | +} { |
| 20 | + const parts = keySpec.split("+"); |
| 21 | + const result: { |
| 22 | + key: string; |
| 23 | + ctrlKey?: boolean; |
| 24 | + metaKey?: boolean; |
| 25 | + shiftKey?: boolean; |
| 26 | + altKey?: boolean; |
| 27 | + } = { |
| 28 | + key: "", // Will be set when we find the actual key |
| 29 | + }; |
| 30 | + |
| 31 | + for (const part of parts) { |
| 32 | + const normalizedPart = part.toLowerCase(); |
| 33 | + |
| 34 | + switch (normalizedPart) { |
| 35 | + case "ctrl": |
| 36 | + case "control": |
| 37 | + result.ctrlKey = true; |
| 38 | + break; |
| 39 | + case "meta": |
| 40 | + case "cmd": |
| 41 | + case "command": |
| 42 | + result.metaKey = true; |
| 43 | + break; |
| 44 | + case "shift": |
| 45 | + result.shiftKey = true; |
| 46 | + break; |
| 47 | + case "alt": |
| 48 | + case "option": |
| 49 | + result.altKey = true; |
| 50 | + break; |
| 51 | + default: |
| 52 | + result.key = part; // Keep original case for the actual key |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return result; |
| 57 | +} |
| 58 | + |
| 59 | +// Helper function to check if event matches the key specification |
| 60 | +function matchesKeySpec(event: KeyboardEvent, keySpec: string): boolean { |
| 61 | + const spec = parseKeySpec(keySpec); |
| 62 | + |
| 63 | + // Check the main key |
| 64 | + if (spec.key && event.key !== spec.key) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + // Ensure modifiers match exactly. If not in spec, they must be false. |
| 69 | + if (event.ctrlKey !== (spec.ctrlKey ?? false)) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + if (event.metaKey !== (spec.metaKey ?? false)) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + if (event.shiftKey !== (spec.shiftKey ?? false)) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + if (event.altKey !== (spec.altKey ?? false)) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | +export function useKeyPress( |
| 86 | + key: string | string[], |
| 87 | + callback: (event: KeyboardEvent) => void, |
| 88 | + options: UseKeyPressOptions = {} |
| 89 | +) { |
| 90 | + const { |
| 91 | + event = "keydown", |
| 92 | + target = typeof window !== "undefined" ? window : null, |
| 93 | + eventOptions, |
| 94 | + } = options; |
| 95 | + |
| 96 | + const callbackRef = useRef(callback); |
| 97 | + |
| 98 | + // Keep callback ref up to date |
| 99 | + useEffect(() => { |
| 100 | + callbackRef.current = callback; |
| 101 | + }); |
| 102 | + |
| 103 | + useEffect(() => { |
| 104 | + if (!target) return; |
| 105 | + |
| 106 | + const handler = (evt: Event) => { |
| 107 | + const event = evt as KeyboardEvent; |
| 108 | + const keys = Array.isArray(key) ? key : [key]; |
| 109 | + |
| 110 | + // Check if any of the specified keys match |
| 111 | + for (const keySpec of keys) { |
| 112 | + if (matchesKeySpec(event, keySpec)) { |
| 113 | + callbackRef.current(event); |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + const targetElement = target as EventTarget; |
| 120 | + targetElement.addEventListener(event, handler, eventOptions); |
| 121 | + |
| 122 | + return () => { |
| 123 | + targetElement.removeEventListener(event, handler, eventOptions); |
| 124 | + }; |
| 125 | + }, [key, target, event, eventOptions]); |
| 126 | +} |
0 commit comments