Skip to content

Instantly share code, notes, and snippets.

@tofusoup429
Last active November 7, 2021 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tofusoup429/4aa866ef8925e26648d02e253c7a3a57 to your computer and use it in GitHub Desktop.
Save tofusoup429/4aa866ef8925e26648d02e253c7a3a57 to your computer and use it in GitHub Desktop.
const KeyDown = () => {
useEffect(() => {
window.addEventListener('keydown', handleKeydown);
return () => removeEventListener('keydown', handleKeydown)
}, []);
const doSomething1 = () => {
console.log('doSomething1');
}
const doSomething2 = () => {
console.log('doSomething2');
}
const handleKeydown = (e: any) => {
console.log('keydown', e.key);
handleKey(e.key);
if(e.key === 'Enter') doSomething1();
else if(e.key === 'Shift') doSomething2();
//One of your functions is executed depending on which key user enters.
}
return(
<div>What Key is Entered?</div>
)
export default KeyDown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment