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?
This question is related to
jquery
keypress
keydown
jquery-events
$(window).keypress("c", function(e) {
if (!e.ctrlKey)
return;
console.info("CTRL + C detected !");
});
$(window).keypress("c", function(e) {_x000D_
if (!e.ctrlKey)_x000D_
return;_x000D_
_x000D_
$("div").show();_x000D_
});
_x000D_
/*https://gist.github.com/jeromyanglim/3952143 */_x000D_
_x000D_
kbd {_x000D_
white-space: nowrap;_x000D_
color: #000;_x000D_
background: #eee;_x000D_
border-style: solid;_x000D_
border-color: #ccc #aaa #888 #bbb;_x000D_
padding: 2px 6px;_x000D_
-moz-border-radius: 4px;_x000D_
-webkit-border-radius: 4px;_x000D_
border-radius: 4px;_x000D_
-moz-box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2), 0 0 0 1px #ffffff inset;_x000D_
-webkit-box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2), 0 0 0 1px #ffffff inset;_x000D_
box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2), 0 0 0 1px #ffffff inset;_x000D_
background-color: #FAFAFA;_x000D_
border-color: #CCCCCC #CCCCCC #FFFFFF;_x000D_
border-style: solid solid none;_x000D_
border-width: 1px 1px medium;_x000D_
color: #444444;_x000D_
font-family: 'Helvetica Neue', Helvetica, Arial, Sans-serif;_x000D_
font-size: 11px;_x000D_
font-weight: bold;_x000D_
white-space: nowrap;_x000D_
display: inline-block;_x000D_
margin-bottom: 5px;_x000D_
}
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
<div style="display:none">_x000D_
<kbd>CTRL</kbd> + <kbd>C</kbd> detected !_x000D_
</div>
_x000D_
There is a plugin for Jquery called "Hotkeys" which allows you to bind to key down combinations.
Does this do what you are after?
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') ; }
}
});
Simple way to do it in 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!");
});
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");
});
});
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'); });
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');
}
});
<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>
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.
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" );
}
});
Source: Stackoverflow.com