为DIV处理`onKeyup'
我想处理键盘上的键以运行一个功能,我想做的是:
<div className="playerContainer" onKeyUp={gameState.toggleCP}></div>
但是没有好处(DIV在整个页面上缩放),我也试图将处理程序添加到默认应用程序类组件,该怎么办?
I want to handle a key clicked on the keyboard to run a function, I'm trying to do:
<div className="playerContainer" onKeyUp={gameState.toggleCP}></div>
but there is no benefit (the div is scaled on the whole page), also I tried to add the handler to the default App
class component, what to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,问题是
gameState.togglecp
未在键
事件上调用。键
为了使事件处理程序在元素上调用 - 元素必须焦点。TL:DR;将
键
侦听器添加到文档
如果要处理
键>键
事件,无论焦点是哪个元素 - 在React App App组件中,请执行以下操作:祝你好运弗伦!
as i understand, the problem is that
gameState.toggleCP
is not being called onkeyup
event.keyup
in order for event handler will be called on an element — element must be in focus.TL:DR; add
keyup
listener onto thedocument
if you want to handle
keyup
event no matter which element is in focus — within React app component do the following:good luck fren!