Lee Quarella Lee Quarella - 7 days ago 7
Javascript Question

Javascript numberpad keycode parsing

I am attempting to parse keydown events from the numberpad with the following:

$('#myDiv').keydown(function(e) {
val = String.fromCharCode(e.which)
});


The problem is that keypad
0-9
return keyCodes of
96-105
which, according to
fromCharCode()
are the lower case letters
a-i
. How do I get keydown events of the numberpad to resolve to the appropriate number?

Answer

You don't: you use the keypress event. The keypress event is the only event that will give you reliable information about typed characters. Your existing code will work if you simply change keydown to keypress.