cmd + s和ctrl + s的jQuery keypress事件 [英] jquery keypress event for cmd+s AND ctrl+s

查看:116
本文介绍了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;
});

这篇关于cmd + s和ctrl + s的jQuery keypress事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆