jquery:keypress,ctrl + c(或类似的一些组合) [英] jquery: keypress, ctrl+c (or some combo like that)

查看:93
本文介绍了jquery:keypress,ctrl + c(或类似的一些组合)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我正在制作的网站上创建快捷方式。我知道我可以这样做:

I'm trying to create shortcuts on the website i'm making. I know I can do it this way:

if(e.which == 17) isCtrl=true;
if(e.which == 83 && isCtrl == true) {
    alert('CTRL+S COMBO WAS PRESSED!')
    //run code for CTRL+S -- ie, save!
    e.preventDefault();
}

但下面的示例更简单,代码更少,但它不是组合键盘事件:

But the example below is easier and less code, but it's not a combo keypress event:

$(document).keypress("c",function() {
  alert("Just C was pressed..");
});

所以我想知道如果使用第二个例子,我可以做类似的事情:

So I wanna know if by using this second example, I could do something like:

$(document).keypress("ctrl+c",function() {
  alert("Ctrl+C was pressed!!");
});

这可能吗?我试过了它并没有用,我做错了什么。

is this possible? I've tried it and it didn't work, what am i doing wrong.

推荐答案

另一种方法(不需要插件)它只使用 .ctrlKey 事件对象的a>属性。它指示是否<在事件发生时按下了kbd> Ctrl ,如下所示:

Another approach (no plugin needed) it to just use .ctrlKey property of the event object that gets passed in. It indicates if Ctrl was pressed at the time of the event, like this:

$(document).keypress("c",function(e) {
  if(e.ctrlKey)
    alert("Ctrl+C was pressed!!");
});

这篇关于jquery:keypress,ctrl + c(或类似的一些组合)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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