Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Correct keyCode for keypad(numpad) keys

I'm getting codes [96..105] by calling String.fromCharCode(event.keyCode) when pressing keys [0..9](digits) on the keypad. Though these codes correspond to characters: 'a b c d e f g h i' instead of [0..9].

Question:

I have 3 inputs in the form. User allowed to enter only in the 1-st input. While user press keys on keyboard some function need to filter it and write it to 2-nd input if pressed key is digit otherwise it must write it to the 3-rd input. How it can be corrected?

My implementation in JSFiddle

like image 309
AEMLoviji Avatar asked Apr 12 '11 05:04

AEMLoviji


People also ask

Where are the numpad keys?

Alternatively referred to as the 10-key, number pad, numeric keyboard, numerical keypad, numpad, or ten key, the numeric keypad is a 17-key keypad on the far right side of a PC keyboard. A numeric keypad may also be a separate device that connects to a computer.

Are numpad keys different?

Each key has its own key press code which software converts into a character. The top row keys have key press codes – 48 (Zero) to 57 (Nine) which are the same values as the ASCII system. But the number keypad has different key press codes – 96 (Zero) to 105 (Nine).


1 Answers

Use the keypress handler:

[somelement].onkeypress = function(e){   e = e || event;   console.log(String.fromCharCode(e.keyCode)); } 

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))

like image 120
KooiInc Avatar answered Sep 28 '22 01:09

KooiInc