keypress - jQuery library for getting keyboard key pressed from code -
i'm having problems function turns keycodes keyboard keys. had giant switch statement, , if code were, say, 37, program output "left arrow key." problem is, different browsers not firing keypresses, , codes mixed up. instance, shift + 7 on mac running chrome outputs code 37, left arrow keypress. firefox on mac not tell me if tab key pressed, etc.
here code 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 = " 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, there libraries jquery can accurately give me correctly-pressed key?
i don't know of library want, switch work correct data.
keypress() returns actual text entry. keyup() returns key code. you'll need examine event.where key, event.shiftkey, event.metakey, event.ctrlkey modifiers.
from http://api.jquery.com/keyup/
to determine key pressed, examine event object passed handler function. while browsers use differing properties store information, jquery normalizes .which property can reliably use retrieve key code. code corresponds key on keyboard, including codes special keys such arrows. catching actual text entry, .keypress() may better choice.
Comments
Post a Comment