我想知道是否有可能用键盘箭头直接在页面上激活事件,而不需要在输入中使用鼠标或类似的东西,我尝试了一下,但它不起作用。
keyPress (e){
if(e.key == "ArrowLeft"){
console.log(e.key)
}
}发布于 2018-04-08 02:24:54
是的,你能做到的。但当您调用它时,它不是在keyDown上而是在keyPress上。
您可以在componentDidMount中添加监听keypress的eventListener。
componentDidMount(){
window.addEventListener('keydown', this.keyPress);
}
componentWillUnmount(){
window.removeEventListener('keydown', this.keyPress);
}
keyPress (e){
if(e.key == "ArrowLeft"){
console.log(e.key)
}
}
https://stackoverflow.com/questions/49710364
复制相似问题