admin管理员组文章数量:1418627
I am familiar with React and its event system, but I can't seem to get the onKeyPress event to fire on a <canvas>
element. In fact, I can't get it to fire on a <div>
either.
Here is the relevant code
class MyComponent extends React.Component {
render() {
return (
<canvas onKeyPress={() => console.log('fired')} />
)
}
}
It works fine if I change the <canvas>
to be an <input>
, but does not work for <div>
. Does this mean that react simply does not support keyPress events on canvas elements? What am I overlooking?
I am familiar with React and its event system, but I can't seem to get the onKeyPress event to fire on a <canvas>
element. In fact, I can't get it to fire on a <div>
either.
Here is the relevant code
class MyComponent extends React.Component {
render() {
return (
<canvas onKeyPress={() => console.log('fired')} />
)
}
}
It works fine if I change the <canvas>
to be an <input>
, but does not work for <div>
. Does this mean that react simply does not support keyPress events on canvas elements? What am I overlooking?
2 Answers
Reset to default 6Just assign tabIndex
to the element for getting the focus.
<canvas tabIndex="0" onKeyPress={ () => console.log( 'fired' ) } />
canvas.addEventListener('keydown', function(event) {
alert('keydown');
}, false);
Check if you can fire the above event :)
本文标签: javascriptHow do I set up an 39onKeyPress39 handler on a ltcanvasgt in reactStack Overflow
版权声明:本文标题:javascript - How do I set up an 'onKeyPress' handler on a <canvas> in react? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745289951a2651731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论