I wonder if this …
inputs.keydown(function (e) {
switch (e.keyCode) {
case 13: //Enter
case 16: //Shift
case 17: //Ctrl
case 18: //Alt
case 19: //Pause/Break
case 20: //Caps Lock
case 27: //Escape
case 35: //End
case 36: //Home
case 37: //Left
case 38: //Up
case 39: //Right
case 40: //Down
// Mac CMD Key
case 91: //Safari, Chrome
case 93: //Safari, Chrome
case 224: //Firefox
break;
default:
$(this).addClass(fill);
break;
}
});
… is also possible with fewer lines?
I know I could do an if-condition, but I wonder if I missed something like case 13 && 16 && …
Maybe some of you know a better practice to check all the cases and write fewer lines of code. I'm just wondering.
Thank you in advance!
I wonder if this …
inputs.keydown(function (e) {
switch (e.keyCode) {
case 13: //Enter
case 16: //Shift
case 17: //Ctrl
case 18: //Alt
case 19: //Pause/Break
case 20: //Caps Lock
case 27: //Escape
case 35: //End
case 36: //Home
case 37: //Left
case 38: //Up
case 39: //Right
case 40: //Down
// Mac CMD Key
case 91: //Safari, Chrome
case 93: //Safari, Chrome
case 224: //Firefox
break;
default:
$(this).addClass(fill);
break;
}
});
… is also possible with fewer lines?
I know I could do an if-condition, but I wonder if I missed something like case 13 && 16 && …
Maybe some of you know a better practice to check all the cases and write fewer lines of code. I'm just wondering.
Thank you in advance!
Share Improve this question asked May 12, 2012 at 8:01 mattmatt 44.3k107 gold badges268 silver badges402 bronze badges 1-
1
I personally think its better than a series of
OR
s andAND
s usingif
s (which would be longer) – Joseph Commented May 12, 2012 at 8:04
4 Answers
Reset to default 7Just put the codes into an array, and then you can simply check if the value is in the array. Since you are using jQuery, you already have an inArray()
method to do this.
var keycodes = [13, 16, 17, 18, 19]; //and so on
//if the keycode is not found in the array, the result will be -1
if ($.inArray(e.keyCode, keycodes) === -1) {
$(this).addClass(fill);
}
You could create a "map" of the keys you don't want to handle - map lookups should be somewhere between O(1)
and O(log n)
, depending on the implementation.
var specialKeys = {
13: 1, // Enter
16: 1, // Shift
...
224: 1 // Cmd/FF
};
inputs.keydown(function (e) {
if (e.keyCode in specialKeys) return;
$(this).addClass(fill);
});
Alternatively as your "keys" are all integers you could fill an array where the indexes are the key code values.
EDIT removed the strings, as suggested by @bažmegakapa
What you have is fine. It's clear, and it'll get handled fairly efficiently. Alternately, you can do it with Array#indexOf
:
var list1 = [
13, //Enter
16, //Shift
17, //Ctrl
18, //Alt
19, //Pause/Break
20, //Caps Lock
27, //Escape
35, //End
36, //Home
37, //Left
38, //Up
39, //Right
40 //Down
];
var list2 = [
91, //Safari, Chrome
93, //Safari, Chrome
224 //Firefox
];
inputs.keydown(function (e) {
if (list1.indexOf(e.keyCode) !== -1) {
// ...
}
else if (list2.indexOf(e.keyCode) !== -1) {
// ...
}
else {
$(this).addClass(fill);
}
});
But it's only fewer "lines" of code if you lose the ments, and the ments seem (to me) to be important.
Note that some quite old browsers won't have Array#indexOf
and so you may have to shim it (which is easy enough to do). Or use jQuery.inArray
instead, since you're using jQuery.
IMHO it's more clear to use the code you pointed out, but for your convenience you can use the conditions inside the case...
case (condition):
//code
break;
Personally I would put all the possible matches into an array and use in_array() from phpjs to check if the value to be tested is there.