I'm having some problems with my function that turns keycodes into keyboard keys. I originally had a giant switch statement, and if the code were, say, 37, my program would output "left arrow key." My problem is, different browsers are not firing certain keypresses, and sometimes the codes get mixed up. For instance, shift + 7 on a mac running Chrome outputs the code 37, which is a left arrow keypress. Firefox on mac does not tell me if the tab key is pressed, etc.
Here is the code I was working with:
function getKey(code) {
var keyPress;
// In case of special keys
switch (code)
{
case 8:
keyPress = " backspace ";
break;
case 9:
keyPress = " tab ";
break;
case 13:
keyPress = " enter ";
break;
case 16:
keyPress = " shift ";
break;
case 17:
keyPress = " control ";
break;
case 18:
keyPress = " alt ";
break;
case 20:
keyPress = " caps lock ";
break;
case 27:
keyPress = " escape ";
break;
case 46:
keyPress = " delete ";
break;
case 37:
keyPress = " left arrow key ";
break;
case 38:
keyPress = " up arrow key ";
break;
case 39:
keyPress = " right arrow key ";
break;
case 40:
keyPress = " down arrow key ";
break;
case 45:
keyPress = " insert ";
break;
case 46:
keyPress = " delete ";
break;
case 91:
keyPress = " command ";
break;
default:
keyPress = String.fromCharCode(code);
}
return keyPress;
}
$(document).keypress(function(e) {
var code = e.which;
var keyPress = String.fromCharCode(code);
$(".keystrokes").append(keyPress);
});
So, are there any libraries for jquery that can accurately give me the correctly-pressed key?