Using Keyboard Shortcuts in Javascript
16
Jun1
Jun1
New type of web sites use keyboards for some functions. It is always helping more usable. I hope this codes will help your web site.
In the following example, we’re simply going to verify the key pressed down by the user. If the key pressed are Ctrl+S, a function will be triggered.
var isCtrl = false;
document.onkeyup=function(e) {
if(e.which == 17) isCtrl=false;
}document.onkeydown=function(e){
if(e.which == 17) isCtrl=true;
if(e.which == 83 && isCtrl == true) {
alert('Keyboard shortcuts are cool!');
return false;
}
}
Example with the JQuery framework
var isCtrl = false;$(document).keyup(function (e) {
if(e.which == 17) isCtrl=false;
}).keydown(function (e) {
if(e.which == 17) isCtrl=true;
if(e.which == 83 && isCtrl == true) {
alert('Keyboard shortcuts + JQuery are even more cool!');
return false;
}
});
For more and keyboard shorcuts codes click here>

