Skip to content

Instantly share code, notes, and snippets.

@bernatfortet
Created April 26, 2017 23:18
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 bernatfortet/28786dc40f601aca0c1efef3ea75068a to your computer and use it in GitHub Desktop.
Save bernatfortet/28786dc40f601aca0c1efef3ea75068a to your computer and use it in GitHub Desktop.
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