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.
这是可能的吗?我试过了,但是没用,我做错了什么。
8 个解决方案
#1
70
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:
另一种方法(不需要插件)只是使用传入的事件对象的. ctrlkey属性。它指示在事件发生时是否按下Ctrl,如下所示:
$(document).keypress("c",function(e) {
if(e.ctrlKey)
alert("Ctrl+C was pressed!!");
});
#2
30
I am a little late to the party but here is my part
我来晚了一点,但这是我的部分
$(document).on('keydown', function ( e ) {
// You may replace `c` with whatever key you want
if ((e.metaKey || e.ctrlKey) && ( String.fromCharCode(e.which).toLowerCase() === 'c') ) {
console.log( "You pressed CTRL + C" );
}
});
#3
5
Try the Jquery Hotkeys plugin instead - it'll do everything you require.
试试Jquery热键插件——它可以做你需要的一切。
jQuery Hotkeys is a plug-in that lets you easily add and remove handlers for keyboard events anywhere in your code supporting almost any key combination.
jQuery Hotkeys是一个插件,它可以让您轻松添加和删除代码中几乎支持任何键组合的任何地方的键盘事件处理程序。
This plugin is based off of the plugin by Tzury Bar Yochay: jQuery.hotkeys
这个插件基于zury Bar Yochay: jQuery.hotkeys的插件
The syntax is as follows:
语法如下:
$(expression).bind(types, keys, handler); $(expression).unbind(types, handler);
$(document).bind('keydown', 'ctrl+a', fn);
// e.g. replace '$' sign with 'EUR'
// $('input.foo').bind('keyup', '$', function(){
// this.value = this.value.replace('$', 'EUR'); });
#4
3
You cannot use Ctrl+C by jQuery , but you can with another library which is shortcut.js
jQuery不能使用Ctrl+C,但可以使用另一个库shortcut.js
Live Demo : Abdennour JSFiddle
现场演示:Abdennour JSFiddle
$(document).ready(function() {
shortcut.add("Ctrl+C", function() {
$('span').html("أحسنت. لقد ضغطت على حرفي : Ctrl+C");
});
shortcut.add("Ctrl+V", function() {
$('span').html("أحسنت. لقد ضغطت على حرفي : Ctrl+V");
});
shortcut.add("Ctrl+X", function() {
$('span').html("أحسنت. لقد ضغطت على حرفي : Ctrl+X");
});
});
#5
1
There is a plugin for Jquery called "Hotkeys" which allows you to bind to key down combinations.
Jquery有一个叫做“热键”的插件,它允许你绑定到按键组合。
Does this do what you are after?
这能达到你想要的效果吗?
Jquery热键-谷歌代码
#6
1
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>
var ctrlMode = false; // if true the ctrl key is down
///this works
$(document).keydown(function(e){
if(e.ctrlKey){
ctrlMode = true;
};
});
$(document).keyup(function(e){
ctrlMode = false;
});
</script>
#7
0
In my case, I was looking for a keydown ctrl key and click event. My jquery looks like this:
在我的例子中,我正在查找按下ctrl键并单击事件。我的jquery是这样的:
$('.linkAccess').click( function (event) {
if(true === event.ctrlKey) {
/* Extract the value */
var $link = $('.linkAccess');
var value = $link.val();
/* Verified if the link is not null */
if('' !== value){
window.open(value);
}
}
});
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.
“linkAccess”是某些特定字段的类名,其中我有一个链接,我想使用key和click的组合来访问它。
#8
0
I couldn't get it to work with .keypress(), but it worked with the .keydown() function like this:
我无法让它与.keypress()一起工作,但它与.keydown()函数一起工作,如下所示:
$(document).keydown(function(e) {
if(e.key == "c" && e.ctrlKey) {
console.log('ctrl+c was pressed');
}
});
#1
70
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:
另一种方法(不需要插件)只是使用传入的事件对象的. ctrlkey属性。它指示在事件发生时是否按下Ctrl,如下所示:
$(document).keypress("c",function(e) {
if(e.ctrlKey)
alert("Ctrl+C was pressed!!");
});
#2
30
I am a little late to the party but here is my part
我来晚了一点,但这是我的部分
$(document).on('keydown', function ( e ) {
// You may replace `c` with whatever key you want
if ((e.metaKey || e.ctrlKey) && ( String.fromCharCode(e.which).toLowerCase() === 'c') ) {
console.log( "You pressed CTRL + C" );
}
});
#3
5
Try the Jquery Hotkeys plugin instead - it'll do everything you require.
试试Jquery热键插件——它可以做你需要的一切。
jQuery Hotkeys is a plug-in that lets you easily add and remove handlers for keyboard events anywhere in your code supporting almost any key combination.
jQuery Hotkeys是一个插件,它可以让您轻松添加和删除代码中几乎支持任何键组合的任何地方的键盘事件处理程序。
This plugin is based off of the plugin by Tzury Bar Yochay: jQuery.hotkeys
这个插件基于zury Bar Yochay: jQuery.hotkeys的插件
The syntax is as follows:
语法如下:
$(expression).bind(types, keys, handler); $(expression).unbind(types, handler);
$(document).bind('keydown', 'ctrl+a', fn);
// e.g. replace '$' sign with 'EUR'
// $('input.foo').bind('keyup', '$', function(){
// this.value = this.value.replace('$', 'EUR'); });
#4
3
You cannot use Ctrl+C by jQuery , but you can with another library which is shortcut.js
jQuery不能使用Ctrl+C,但可以使用另一个库shortcut.js
Live Demo : Abdennour JSFiddle
现场演示:Abdennour JSFiddle
$(document).ready(function() {
shortcut.add("Ctrl+C", function() {
$('span').html("أحسنت. لقد ضغطت على حرفي : Ctrl+C");
});
shortcut.add("Ctrl+V", function() {
$('span').html("أحسنت. لقد ضغطت على حرفي : Ctrl+V");
});
shortcut.add("Ctrl+X", function() {
$('span').html("أحسنت. لقد ضغطت على حرفي : Ctrl+X");
});
});
#5
1
There is a plugin for Jquery called "Hotkeys" which allows you to bind to key down combinations.
Jquery有一个叫做“热键”的插件,它允许你绑定到按键组合。
Does this do what you are after?
这能达到你想要的效果吗?
Jquery热键-谷歌代码
#6
1
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>
var ctrlMode = false; // if true the ctrl key is down
///this works
$(document).keydown(function(e){
if(e.ctrlKey){
ctrlMode = true;
};
});
$(document).keyup(function(e){
ctrlMode = false;
});
</script>
#7
0
In my case, I was looking for a keydown ctrl key and click event. My jquery looks like this:
在我的例子中,我正在查找按下ctrl键并单击事件。我的jquery是这样的:
$('.linkAccess').click( function (event) {
if(true === event.ctrlKey) {
/* Extract the value */
var $link = $('.linkAccess');
var value = $link.val();
/* Verified if the link is not null */
if('' !== value){
window.open(value);
}
}
});
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.
“linkAccess”是某些特定字段的类名,其中我有一个链接,我想使用key和click的组合来访问它。
#8
0
I couldn't get it to work with .keypress(), but it worked with the .keydown() function like this:
我无法让它与.keypress()一起工作,但它与.keydown()函数一起工作,如下所示:
$(document).keydown(function(e) {
if(e.key == "c" && e.ctrlKey) {
console.log('ctrl+c was pressed');
}
});