按键,Ctrl + C(或类似的组合键) [英] keypress, ctrl+c (or some combo like that)

查看:191
本文介绍了按键,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 want to 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 属性""rel =" noreferrer>事件对象传入.它指示在事件发生时是否按下了 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!!");
});

这篇关于按键,Ctrl + C(或类似的组合键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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