[ACCEPTED]-Get Correct keyCode for keypad(numpad) keys-jquery
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 2 use the keyup
or keydown
handler, you can subtract 48 1 from e.keyCode
to get the number (so String.fromCharCode(e.keyCode-48)
)
I fixed the issue using following javascript 3 code. The numpad keys lie between 96
to 105
. but 2 the real numbers are less 48
from the numpad 1 values. Works with keyup or keydown handler.
var keyCode = e.keyCode || e.which;
if (keyCode >= 96 && keyCode <= 105) {
// Numpad keys
keyCode -= 48;
}
var number = String.fromCharCode(keyCode);
There is a way to do this with keydown, if 15 keypress is not workable due to event canceling 14 needs, etc. Use an if() statement with this 13 test:
parseInt(event.keyIdentifier.substring(2),16) > 47 && parseInt(event.keyIdentifier.substring(2),16) < 58
OR, with jQuery events:
parseInt(event.originalEvent.keyIdentifier.substring(2),16) > 47 && parseInt(event.originalEvent.keyIdentifier.substring(2),16) < 58
These examples 12 assume "event" is the keydown event. keyIdentifier 11 is a hexidecimal number that represents 10 the unicode value for the related char. Using 9 keyIdentifier, numbers from the numberpad 8 / keypad AND the numbers above your QWERTY 7 keyboard will all have the same values, 48 6 - 57 (U+0030 - U+0039), even with the keyDown 5 event.
Unicode values in the browsers will 4 look like U+0030 or U+002F. Parse this string 3 to only get the hexidecimal value, then 2 use parseInt() with a radix of 16 to convert 1 it to base-10.
Ok. But In Firfox Gecko It doesn't work. I 1 use bellow code and I'm happy with it :)
[somelement].onkeypress = function(e){
var singleChar=e.key || String.fromCharCode(e.keyCode);
console.log(singleChar);
}
event.charCode at onKeyPress return the 6 same code when press a number in keyboard 5 and keypad. but event.keyCode at onKeyDown 4 (or up) return different code. => get char: use 3 event.charCode at onKeyPress event event.preventDefault() (or 2 event.returnValue=false on IE) for number 1 use event.keyCode at onKeyDown event
I think a safer way is not assuming the 7 value keys that will be used. You could 6 get the working value by the KeyboardEvent 5 object and access its properties "DOM_VK_*". Let 4 me give you an example.
const kcN0=KeyboardEvent.DOM_VK_0;//get the 0 key value (commonly,48)
const kcN9=KeyboardEvent.DOM_VK_9;//get the 9 key value (commonly, 57)
const kcNPad0=KeyboardEvent.DOM_VK_NUMPAD0;//get the NUMPAD 0 key value (commonly,96)
const kcNPad9=KeyboardEvent.DOM_VK_NUMPAD9;//get the NUMPAD 9 key value (commonly,105)
const kcNPad_L=KeyboardEvent.DOM_KEY_LOCATION_NUMPAD;//get the key location of NUMPAD (commonly, 3)
and evalute the event 3 variable "kbEv"
let pressKeyCode=(kbEv.which||kbEv.keyCode);
if( kbEv.location===kcNPad_L && pressKeyCode>=kcNumP0 && pressKeyCode<=kcNumP9 )
pressKeyCode=kcN0+kcNPad9-pressKeyCode;
I've only assumed that from 2 0 to 9 will be sequential in those two disjoint 1 numerical digit pattern set.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.