I need to figure out which character was typed into a text field from within the handler that is called by jQuery's keydown
function. key.which
gives me only the keycode, but I need to figure out which ASCII character key
represents. How do I do this?
views:
625answers:
1
+4
A:
For character input, it is suggested you use keypress()
, which will report the actual ASCII code for the character pressed. It automatically takes care of letter case, and ignores non-character presses. In either case, you can use fromCharCode() to convert to a string representation. E.g.
var c = String.fromCharCode(e.which) // or e.keyCode
Just remember that for keydown()
and keyup()
, you'll have to keep track of the case using the e.shiftKey
state.
Max Shawabkeh
2010-02-08 08:09:19
doesn't work for me in firefox. I get 1/4 when I press ,
Anders Rune Jensen
2010-06-10 15:31:26
haha.. for some reason, browsers are returning 188 when comma is pressed, but when we convert 188 to char, it is coming as ¼. Interestingly, when we convert 44 to char, browsers are understanding it as comma!!
Senthil
2010-06-25 12:11:59