So I wrote this script so that you can have keyboard shortcuts on your website, and I was wondering how to do multiple keys (ie instead of doing just the “left arrow” key, it would be the “ctrl + left arrow”. Here’s my current syntax:
var arrow = {
left: 37,
up: 38,
right: 39,
down: 40
};
function DoSomething() {}
$(document).ready(function() { // requires jQuery
$("body").keydown(function(event) {
if(event.keyCode == arrow.left) {
DoSomething();
}
}
}
But what I would like to do is something like this:
var arrow = {
left: 37,
up: 38,
right: 39,
down: 40
},
ctrl = 17;
function DoSomething() {}
$(document).ready(function() { // requires jQuery
$("body").keydown(function(event) {
if(event.keyCode == ctrl && arrow.left) {
DoSomething();
}
}
}
The event object provided in jQuery tells you if the
ctrlkey is being pressed.Demo: http://jsfiddle.net/zcMXR/