This is what I have now:
$("input").bind("keydown",function(e){
var value = this.value + String.fromCharCode(e.keyCode);
}
If the e.keyCode
may not be an ASCII character (Alt, backspace, del, arrows, etc.)…
I would now need to trim
these values from value
somehow (preferably programmatically – not with lookup tables).
I'm using jQuery.
I must use the keydown
event. keyPress
doesn't activate for certain keys I need to capture (Esc, del, backspace, etc.).
I cannot use setTimeout
to get the input's value. setTimeout(function(){},0)
is too slow.
Best Answer
In my experience
String.fromCharCode(e.keyCode)
is unreliable.String.fromCharCode
expects unicode charcodes as an argument;e.keyCode
returns javascript keycodes. Javascript keycodes and unicode charcodes are not the same thing! In particular, the numberpad keys return a differentkeycode
from the ordinary number keys (since they are different keys) while the samekeycode
is returned for bothupper
andlowercase
letters (you pressed the same key in both cases), despite them having differentcharcodes
.For example, the ordinary number key 1 generates an event with
keycode
49 while numberpad key 1 (with Numlock on) generateskeycode
97. Used withString.fromCharCode
we get the following:String.fromCharCode
expects unicode charcodes, not javascript keycodes. The key a generates an event with akeycode
of 65, independentant of the case of the character it would generate (there is also a modifier for if the Shift key is pressed, etc. in the event). The character a has a unicodecharcode
of 61 while the character A has acharcode
of 41 (according to, for example, http://www.utf8-chartable.de/). However, those arehex
values, converting to decimal gives us acharcode
of 65 for "A" and 97 for "a".[1] This is consistent with what we get fromString.fromCharCode
for these values.My own requirement was limited to processing numbers and ordinary letters (accepting or rejecting depending on the position in the string) and letting control characters (F-keys, Ctrl-something) through. Thus I can check for the control characters, if it's not a control character I check against a range and only then do I need to get the actual character. Given I'm not worried about case (I change all letters to uppercase anyway) and have already limited the range of keycodes, I only have to worry about the numberpad keys. The following suffices for that:
More generally, a function to reliably return the character from a
charcode
would be great (maybe as a jQuery plugin), but I don't have time to write it just now. Sorry.I'd also mention
e.which
(if you're using jQuery) which normalizese.keyCode
ande.charCode
, so that you don't need to worry about what sort of key was pressed. The problem with combining it withString.fromCharCode
remains.[1] I was confused for a while -. all the docs say that
String.fromCharCode
expects a unicodecharcode
, while in practice it seemed to work for ASCII charcodes, but that was I think due to the need to convert to decimal from hex, combined with the fact that ASCII charcodes and unicode decimal charcodes overlap for ordinary latin letters.