最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Will this hotkey work for Mac users? - Stack Overflow

matteradmin4PV0评论

I'm using the following code to catch when people press Ctrl+Shift+P for my chrome extension:

window.addEventListener("keydown", function(event) {
  var modifier = event.ctrlKey || event.metaKey;
  if (modifier && event.shiftKey && event.keyCode == 80) {
    //code goes here
  }
});

I'm hoping the var modifier = event.ctrlKey || event.metaKey; line means it will catch when Mac users press Cmd-Shift-P but have no Mac puter to test this on. Is it so? Will my hotkey work for Mac users?

Also, what is the keyCode for when the Cmd key goes down and up? Is it 17, the same as for Ctrl?

I'm using the following code to catch when people press Ctrl+Shift+P for my chrome extension:

window.addEventListener("keydown", function(event) {
  var modifier = event.ctrlKey || event.metaKey;
  if (modifier && event.shiftKey && event.keyCode == 80) {
    //code goes here
  }
});

I'm hoping the var modifier = event.ctrlKey || event.metaKey; line means it will catch when Mac users press Cmd-Shift-P but have no Mac puter to test this on. Is it so? Will my hotkey work for Mac users?

Also, what is the keyCode for when the Cmd key goes down and up? Is it 17, the same as for Ctrl?

Share Improve this question edited Apr 28, 2015 at 13:32 Ilya 29.8k19 gold badges119 silver badges161 bronze badges asked Nov 28, 2011 at 21:41 flea whaleflea whale 1,8133 gold badges27 silver badges38 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

Keycodes differ in different browsers. Here you can find more information on the subject: How does one capture a Mac's mand key via JavaScript?

On my mac in Chrome I get the keycode 91 but it will differ, in the linked post these are mentioned: Firefox: 224 Opera: 17 WebKit (Safari/Chrome): 91 (Left Apple) or 93 (Right Apple)

There is also a link to this page which tells all about it: http://unixpapa./js/key.html

Your code does work in Chrome on Mac (pressing the cmd+Shift+P).

Post a comment

comment list (0)

  1. No comments so far