cmd + s和ctrl + s的jQuery keypress事件

问题描述:

使用上一个问题的示例之一:

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;
});

是否也可以将其更改为Mac cmd键?

Is it also possible to change this to work for the Mac cmd key?

我尝试过(!(event.which == 115&&(event.cmdKey || event.ctrlKey))& &!(event.which == 19))但这没有工作。

I have tried (!(event.which == 115 && (event.cmdKey || event.ctrlKey)) && !(event.which == 19)) but this didn't work.

使用 event.metaKey 检测Command键

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;
});