Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
handleKeyDown = () => {
if(event.keyCode == 32)
this.setState({ isSpacebarPressed: true })
}
handleKeyUp = () => {
if(this.state.isSpacebarPressed)
this.setState({ isSpacebarPressed: false })
}
getSpacebarIsPressedClass(){
return this.state.isSpacebarPressed ? 'spacebar' : ''
}
componentDidMount() {
document.addEventListener('keydown', this.handleKeyDown, true)
document.addEventListener('keyup', this.handleKeyUp, true)
}
componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeyDown, true)
document.removeEventListener('keyup', this.handleKeyUp, true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.