jquery keypress event for cmd+s AND ctrl+s

css, html, javascript, jquery, macos

Solution

Use the event.metaKey to detect the Command key

$(document).keypress(function(event) {
    if (event.which == 115 && (event.ctrlKey||event.metaKey)|| (event.which == 19)) {
        event.preventDefault();
        // do stuff
        return false;
    }
    return true;
});

Problem

Using one of the examples from a previous question I have: ``` $(window).keypress(function(event) { if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true; $("form input[name=save]").click(); event.preventDefault(); return false; }); ``` Is it also possible to change this to work for the Mac cmd key? I have tried `(!(event.which == 115 && (event.cmdKey || event.ctrlKey)) && !(event.which == 19))` but this didn't work.

Original source

Related problems