-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GEN-1580]: useKeyDown hook fixed (#1648)
- Loading branch information
1 parent
f3c1a26
commit 3bb2338
Showing
3 changed files
with
119 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,109 @@ | ||
import { useEffect } from "react"; | ||
|
||
export function useKeyDown(key, callback) { | ||
const handleKeyDown = (event) => { | ||
if (key === event.key) { | ||
callback(event); | ||
} | ||
}; | ||
import { useEffect } from 'react'; | ||
|
||
export function useKeyDown(key: Key | null, callback: (e: KeyboardEvent) => void) { | ||
useEffect(() => { | ||
window.addEventListener("keydown", handleKeyDown); | ||
const handleKeyDown = (e: KeyboardEvent) => { | ||
if (key && key === e.key) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
callback(e); | ||
} | ||
}; | ||
|
||
window.addEventListener('keydown', handleKeyDown); | ||
|
||
return () => { | ||
window.removeEventListener("keydown", handleKeyDown); | ||
window.removeEventListener('keydown', handleKeyDown); | ||
}; | ||
}, [key, callback]); | ||
|
||
return handleKeyDown; | ||
return null; | ||
} | ||
|
||
// Chat GPT scraped from: https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values | ||
type Key = | ||
| 'Backspace' | ||
| 'Tab' | ||
| 'Enter' | ||
| 'Shift' | ||
| 'Control' | ||
| 'Alt' | ||
| 'Pause' | ||
| 'CapsLock' | ||
| 'Escape' | ||
| 'Space' | ||
| 'PageUp' | ||
| 'PageDown' | ||
| 'End' | ||
| 'Home' | ||
| 'ArrowLeft' | ||
| 'ArrowUp' | ||
| 'ArrowRight' | ||
| 'ArrowDown' | ||
| 'PrintScreen' | ||
| 'Insert' | ||
| 'Delete' | ||
| '0' | ||
| '1' | ||
| '2' | ||
| '3' | ||
| '4' | ||
| '5' | ||
| '6' | ||
| '7' | ||
| '8' | ||
| '9' | ||
| 'a' | ||
| 'b' | ||
| 'c' | ||
| 'd' | ||
| 'e' | ||
| 'f' | ||
| 'g' | ||
| 'h' | ||
| 'i' | ||
| 'j' | ||
| 'k' | ||
| 'l' | ||
| 'm' | ||
| 'n' | ||
| 'o' | ||
| 'p' | ||
| 'q' | ||
| 'r' | ||
| 's' | ||
| 't' | ||
| 'u' | ||
| 'v' | ||
| 'w' | ||
| 'x' | ||
| 'y' | ||
| 'z' | ||
| 'Meta' | ||
| 'ContextMenu' | ||
| 'F1' | ||
| 'F2' | ||
| 'F3' | ||
| 'F4' | ||
| 'F5' | ||
| 'F6' | ||
| 'F7' | ||
| 'F8' | ||
| 'F9' | ||
| 'F10' | ||
| 'F11' | ||
| 'F12' | ||
| 'NumLock' | ||
| 'ScrollLock' | ||
| 'AudioVolumeMute' | ||
| 'AudioVolumeDown' | ||
| 'AudioVolumeUp' | ||
| 'MediaTrackNext' | ||
| 'MediaTrackPrevious' | ||
| 'MediaStop' | ||
| 'MediaPlayPause' | ||
| 'LaunchMail' | ||
| 'LaunchApp1' | ||
| 'LaunchApp2' | ||
| 'Unidentified'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters