Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will this hotkey work for Mac users?

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 computer 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?

like image 208
flea whale Avatar asked Nov 28 '11 21:11

flea whale


People also ask

Are keyboard shortcuts the same for Mac and Windows?

Although Windows and Mac keyboards feature most of the same keys, there are a few that differ. When using a Windows keyboard on a Mac, the Windows key is used instead of the Command key, and the Alt key is used in place of the Option key. For example, the Undo shortcut would use Windows+Z instead of Command+Z.


1 Answers

Keycodes differ in different browsers. Here you can find more information on the subject: How does one capture a Mac's command 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.com/js/key.html

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

like image 131
span Avatar answered Nov 15 '22 14:11

span