AEMLoviji AEMLoviji -4 years ago 296
Javascript Question

Get Correct keyCode for keypad(numpad) keys

I get keycodes from 96 to 105 when pressing keys on the keypad [0..9]
These keycodes correspond with the characters: 'a b c d e f g h i' instead of '0 1 2 3 4 5 6 7 8 9' when calling String.fromCharCode(event.keyCode).


I have 3 input[type='text']
when user press keys on first input

if(this key is numeric)
write it to second input
else if (this key is alpha)
write it to third input

nut it gives 'a' when i push 1 from numpad.
how solve it?

here is my code

EDIT: i have used keyup event for this reason a got previous char :-)

Answer Source

Use the keypress handler:

[somelement].onkeypress = function(e){
  e = e || event;

See also: this W3C testdocument

if you want to use the keyup or keydown handler, you can subtract 48 from e.keyCode to get the number (so String.fromCharCode(e.keyCode-48))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download