简体   繁体   English

我们如何检测 Macbook Pro 触控栏按键的“keyDown/keyPress”事件?

[英]How do we detect the "keyDown/keyPress" events for Macbook Pro touch bar keys?

I recently came across a requirement to close a Modal on Esc key press (508 compliance).我最近遇到了在 Esc 按键上关闭 Modal 的要求(508 合规性)。 I then realised that the onKeyDown handler I wrote on my react component wasn't working as expected.然后我意识到我在我的 react 组件上编写的 onKeyDown 处理程序没有按预期工作。 The event wasn't getting fired on clicking Escape key from my macbook pro's touch bar.从我的 macbook pro 的触摸栏中单击 Escape 键时不会触发该事件。 Has anyone gone through similar issue?有没有人经历过类似的问题? If yes, is there a workaround?如果是,是否有解决方法?

You should write onKeyDown={(e) => yourFunctionName(e)} on the Modal element and your function should look like something like this你应该在 Modal 元素上写onKeyDown={(e) => yourFunctionName(e)}并且你的 function 应该看起来像这样

const yourFunctionName = (e) => { if (e.key === "ENTER") { closeModal() } }

Make sure Esc key-code is 27确保 Esc 键码为 27

func((e) => e.keyCode === '27'){
  //your code }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM