I would like to be able to log the key presses on a specific page, trying to implement an \'Easter egg\' type functionality where when the correct keys are pressed in the co
In such circumstances it might also be useful to be able to check for the ctl/alt/shift keys:
if (e.altKey) {
}
if (e.ctrlKey) {
}
if (e.shiftKey) {
}
For keypress codes, the following object literal should help:
var Key =
{
BACKSPACE: 8,
TAB: 9,
ENTER: 13,
ESC: 27,
PAGEUP: 33,
PAGEDOWN: 34,
END: 35,
HOME: 36,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
HELP: 47,
H: 72,
K: 75,
N: 78,
R: 82,
NUMERIC_PLUS: 107,
F1: 112,
F2: 113,
F3: 114,
F4: 115,
F5: 116,
F6: 117,
F7: 118,
F8: 119,
F9: 120,
F10: 121,
F11: 122,
F12: 123,
PLUS: 187,
MINUS: 189,
V: 86
}
So instead of:
switch(event.keyCode) {
case 40:
keypresses.push('down');
break;
case 38:
keypresses.push('up');
}
We can say:
switch(event.keyCode) {
case Key.DOWN:
keypresses.push('down');
break;
case Key.UP:
keypresses.push('up');
break;
}
This promotes self documenting code, and is more readable and maintainable.