Last active
November 7, 2021 01:01
-
-
Save tofusoup429/4aa866ef8925e26648d02e253c7a3a57 to your computer and use it in GitHub Desktop.
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
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