最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Apple Touch Bar keys not firing keypress event - Stack Overflow

programmeradmin3浏览0评论

Is there another event I can listen for when I want to listen for an Apple Touch Bar keypress? Specifically, I would like to listen for the Escape keypress event.

My code is working on "regular" keyboards, with actual escape keys, but not with the Touch Bar.

listenForKeypressEvent = (e) => {
    console.log(e);
    if (e.keyCode === 27 || e.key === "Escape") {
        // do the things
    }
}

window.addEventListener('keypress', listenForKeypressEvent);

Is there another event I can listen for when I want to listen for an Apple Touch Bar keypress? Specifically, I would like to listen for the Escape keypress event.

My code is working on "regular" keyboards, with actual escape keys, but not with the Touch Bar.

listenForKeypressEvent = (e) => {
    console.log(e);
    if (e.keyCode === 27 || e.key === "Escape") {
        // do the things
    }
}

window.addEventListener('keypress', listenForKeypressEvent);
Share Improve this question edited Nov 16, 2018 at 21:55 Tiff Nogueira asked Nov 16, 2018 at 21:46 Tiff NogueiraTiff Nogueira 1415 bronze badges 2
  • In which browser? – dolmen Commented Dec 19, 2018 at 17:53
  • Chrome, Firefox. Keydown worked – Tiff Nogueira Commented Dec 20, 2018 at 18:06
Add a ment  | 

2 Answers 2

Reset to default 8

Using keydown instead of keypress works.

window.addEventListener('keydown', listenForKeypressEvent);

Not sure why but in my case keyup worked fine for me

window.addEventListener('keyup', listenForKeypressEvent);
发布评论

评论列表(0)

  1. 暂无评论