KeyboardEvent.keyCode is deprecated and no longer recommended. KeyboardEvent.code or KeyboardEvent.key should be used. KeyboardEvent.code
property represents a physical key on the keyboard. KeyboardEvent.key
property returns the value of the key.
Examples:
KeyboardEvent.code
: If 1
is pressed on Numpad
it will return Numpad1
.
KeyboardEvent.key
: If 1
is pressed on Numpad
it will return 1
.
If you want only to get keys from Numpad
or the Digit
keys, Check if indexOf
Numpad/Digit is greater than -1
. Your function will be as following:
function getKeyVal(e) {
if (e.code.indexOf('Numpad') > -1 || e.code.indexOf('Digit') > -1) {
return e.key;
}
}
Otherwise simply use e.key
or function as:
function getKeyVal(e) {
return e.key;
}