The Internet is full of ideas on this one, but many of them are dated, and many of them are platform specific.
If I want to respond to a keyboard shortcut for save (⌘S on Mac, control-S on Linux/Windows), I know I can try something like this:
document.onkeypress=doit;
function doit(event) {
if(event.metaKey||event.ctrlKey) {
switch(event.key) {
case s: doSomething();
break;
// etc
}
}
}
The problem is the metaKey
should only work on Mac, while ctrlKey
should only work on Linux/Windows. Also, I can’t find a simple answer to whether I should use the keypress
event or keyup
or keydown
.
The question is, what is the preferred way of doing this?