jQuery 按键、ctrl+c(或类似的组合)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4604057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
keypress, ctrl+c (or some combo like that)
提问by android.nick
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?
这可能吗?我试过了,没有用,我做错了什么?
回答by Nick Craver
Another approach (no plugin needed) is to just use .ctrlKey
property of the event objectthat gets passed in. It indicates if Ctrlwas pressed at the time of the event, like this:
另一种方法(不需要插件)是只使用传入.ctrlKey
的事件对象的属性。它指示是否Ctrl在事件发生时被按下,如下所示:
$(document).keypress("c",function(e) {
if(e.ctrlKey)
alert("Ctrl+C was pressed!!");
});
回答by Kamran Ahmed
I am a littlelate 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" );
}
});
回答by Andy
Try the Jquery Hotkeysplugin instead - it'll do everything you require.
尝试使用Jquery Hotkeys插件 - 它会完成您需要的一切。
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.
This plugin is based off of the plugin by Tzury Bar Yochay: jQuery.hotkeys
The syntax is as follows:
jQuery Hotkeys 是一个插件,可让您轻松添加和删除代码中任意位置的键盘事件处理程序,几乎支持任何组合键。
这个插件基于 Tzury Bar Yochay 的插件:jQuery.hotkeys
语法如下:
$(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'); });
回答by Abdennour TOUMI
You cannot use Ctrl+Cby 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");
});
});
回答by Chris
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');
}
});
回答by diagonalbatman
There is a plugin for Jquery called "Hotkeys" which allows you to bind to key down combinations.
Jquery 有一个名为“Hotkeys”的插件,它允许您绑定到按键组合。
Does this do what you are after?
这是否符合您的要求?
回答by Yakir Manor
<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>
回答by A-312
$(window).keypress("c", function(e) {
if (!e.ctrlKey)
return;
console.info("CTRL + C detected !");
});
$(window).keypress("c", function(e) {
if (!e.ctrlKey)
return;
$("div").show();
});
/*https://gist.github.com/jeromyanglim/3952143 */
kbd {
white-space: nowrap;
color: #000;
background: #eee;
border-style: solid;
border-color: #ccc #aaa #888 #bbb;
padding: 2px 6px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
-moz-box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2), 0 0 0 1px #ffffff inset;
-webkit-box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2), 0 0 0 1px #ffffff inset;
box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2), 0 0 0 1px #ffffff inset;
background-color: #FAFAFA;
border-color: #CCCCCC #CCCCCC #FFFFFF;
border-style: solid solid none;
border-width: 1px 1px medium;
color: #444444;
font-family: 'Helvetica Neue', Helvetica, Arial, Sans-serif;
font-size: 11px;
font-weight: bold;
white-space: nowrap;
display: inline-block;
margin-bottom: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="display:none">
<kbd>CTRL</kbd> + <kbd>C</kbd> detected !
</div>
回答by den232
As of 2019, this works (in Chrome at least)
截至 2019 年,这有效(至少在 Chrome 中)
$(document).keypress(function(e) {
var key = (event.which || event.keyCode) ;
if(e.ctrlKey) {
if (key == 26) { console.log('Ctrl+Z was pressed') ; }
else if (key == 25) { console.log('Ctrl+Y was pressed') ; }
else if (key == 19) { console.log('Ctrl+S was pressed') ; }
else { console.log('Ctrl', key, 'was pressed') ; }
}
});
回答by Aniket Warey
Simple way to do it in jQuery :
在 jQuery 中执行此操作的简单方法:
/* The elements we'll bind the shortcut keys to. */
var elements = "body, input, select, checkbox, textarea";
/* Bind the key short-cut 'Ctrl+S' to the save function. */
$(elements).bind ("keydown", "ctrl+space", function (e) {
// Prevent the default operation.
e.preventDefault ();
// Stop processing if we're already doing something.
console.log ("That's right , you pressed correct shortcut!");
});