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
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.
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).
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)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With