jQuery - add hotkeys
You can use keydown
and keyup
like below.
What this code below does is that on keydown
, it sets the value of that key in the object keys
to true. It also uses an if statement to check if those 3 keys are pressed. If so, it does something.
The 17
, 18
, and 49
are the keycodes for CtrlShift1
var keys = {};
$(document).keydown(function(e) { keys[e.which] = true;
if (keys[17] && keys[18] && keys[49]) { // Ctrl + Alt + 1 in that order console.log("pressed"); }});
$(document).keyup(function(e) { delete keys[e.which];});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
How to disable keyboard shortcuts completely from JavaScript
The onkeydown event occurs when the user is pressing a key, you can prevent it using returing false:
Use:
document.onkeydown = function (e) {
return false;
}
jquery : how to use keyboard shortcut F2 and F3
Try using the keydown
event instead of keypress
. The keydown
event tells you which actual key was pressed, but keypress
is more about what character resulted.
And return false
so that the default browser behaviour (if any) for those keys doesn't go ahead (don't return false
for other keys).
$("#ENQUIRY_VIEWMETER").keydown(function(event) {
if(event.which == 113) { //F2
updateMtr();
return false;
}
else if(event.which == 114) { //F3
resetView();
return false;
}
});
Demo: http://jsfiddle.net/TTrPp/
jQuery left right keyboard shortcut to visit through pagination
It appears this is working.
$(document).keydown(function(e) {
var prevPageLink = $( ".prev" ).attr('href');
var nextPageLink = $( ".next" ).attr('href');
switch(e.which) {
case 37:
if ( prevPageLink ) {
window.location = prevPageLink;
}
break;
case 39:
if ( nextPageLink ) {
window.location = nextPageLink;
}
break;
default: return;
}
e.preventDefault();
});
If someone has better method, let me know. :)
Related Topics
Selected Text Event Trigger in JavaScript
Redirect a State to Default Substate with Ui-Router in Angularjs
How to Expose Iframe's Dom Using Jquery
How Does Asynchronous JavaScript Execution Happen? and When Not to Use Return Statement
Difference Between Using Bracket ('[]') and Dot ('.') Notation
Remove Not Alphanumeric Characters from String
How to Execute Promises Sequentially, Passing the Parameters from an Array
Caching a Promise Object in Angularjs Service
Serializing Object That Contains Cyclic Object Value
How to Overload Functions in JavaScript
Chrome Extension - Sendresponse Not Waiting for Async Function