按键、ctrl+c(或类似的组合)
我正在尝试在我正在制作的网站上创建快捷方式。我知道我可以这样做:
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();
}
但是下面的示例更简单且代码更少,但它不是组合按键事件:
$(document).keypress("c",function() {
alert("Just C was pressed..");
});
所以我想知道通过使用第二个示例,我是否可以做类似的事情:
$(document).keypress("ctrl+c",function() {
alert("Ctrl+C was pressed!!");
});
这可能吗?我已经尝试过了,但没有成功,我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
另一种方法(不需要插件)是仅使用
.ctrlKey
传入的 事件对象 的 属性。它指示在事件发生时是否按下 Ctrl,如下所示:Another approach (no plugin needed) is 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:我参加聚会有点,但这是我的部分
I am a little late to the party but here is my part
尝试使用 Jquery Hotkeys 插件 - 它会完成您所需的一切。
Try the Jquery Hotkeys plugin instead - it'll do everything you require.
我无法让它与 .keypress() 一起使用,但它可以与 .keydown() 函数一起使用,如下所示:
I couldn't get it to work with .keypress(), but it worked with the .keydown() function like this:
您不能通过 jQuery 使用 Ctrl+C ,但可以使用另一个库 shortcut.js
现场演示:
阿布登努尔 JSFiddle
You cannot use Ctrl+C by jQuery , but you can with another library which is shortcut.js
Live Demo :
Abdennour JSFiddle
Jquery 有一个名为“Hotkeys”的插件,它允许您绑定按键组合。
这符合你的要求吗?
Jquery 热键 - Google 代码
There is a plugin for Jquery called "Hotkeys" which allows you to bind to key down combinations.
Does this do what you are after?
Jquery HotKeys - Google Code
在 jQuery 中执行此操作的简单方法:
Simple way to do it in jQuery :
就我而言,我正在寻找 keydown ctrl 键和单击事件。
我的 jquery 如下所示:
其中“linkAccess”是某些特定字段的类名称,其中我有一个链接,并且我想使用按键和单击的组合来访问它。
In my case, I was looking for a keydown ctrl key and click event.
My jquery looks like this:
Where "linkAccess" is the class name for some specific fields where I have a link and I want to access it using my combination of key and click.
截至 2019 年,此功能有效(至少在 Chrome 中)
As of 2019, this works (in Chrome at least)