问题
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?
回答1:
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!!");
});
回答2:
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:
Try the Jquery Hotkeys plugin instead - it'll do everything you require.
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:
$(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:
You cannot use Ctrl+C by jQuery , but you can with another library which is shortcut.js
Live Demo : 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:
I couldn't get it to work with .keypress(), but it worked with the .keydown() function like this:
$(document).keydown(function(e) {
if(e.key == "c" && e.ctrlKey) {
console.log('ctrl+c was pressed');
}
});
回答6:
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
回答7:
<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>
回答8:
In my case, I was looking for a keydown ctrl key and click event. My jquery looks like this:
$('.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.
回答9:
$(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>
回答10:
As of 2019, this works (in Chrome at least)
$(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') ; }
}
});
来源:https://stackoverflow.com/questions/4604057/keypress-ctrlc-or-some-combo-like-that