Created
April 26, 2017 23:18
-
-
Save bernatfortet/28786dc40f601aca0c1efef3ea75068a 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
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