| 5 | fn: Function |
| 6 | ) { |
| 7 | const handleKeydown = (e: KeyboardEvent) => { |
| 8 | const ctrl = keyCombination.ctrl && (e.ctrlKey || e.metaKey); |
| 9 | const alt = keyCombination.alt && e.altKey; |
| 10 | const key = keyCombination.caseSensitive |
| 11 | ? keyCombination.key |
| 12 | : keyCombination.key.toLocaleLowerCase(); |
| 13 | |
| 14 | const eKey = keyCombination.caseSensitive ? e.key : e.key.toLocaleLowerCase(); |
| 15 | |
| 16 | const isCombo = () => { |
| 17 | if (ctrl && alt) return eKey === key; |
| 18 | if (ctrl) return eKey === key; |
| 19 | if (alt) return eKey === key; |
| 20 | return false; |
| 21 | }; |
| 22 | |
| 23 | if (isCombo()) { |
| 24 | e.preventDefault(); |
| 25 | fn(); |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | const removeKeydownListener = () => { |
| 30 | document.removeEventListener("keydown", handleKeydown); |