问题
Do the numbers on a numeric keypad have a different keycode than the numbers at the top of a keyboard?
Here is some JavaScript that is supposed to run on the keyup event, but only if the keycode is between 48 and 57. Here is the code:
$(\'#rollNum\').keyup(function(e) {
if(e.keyCode >= 48 && e.keyCode <= 57) { //0-9 only
var max = 15;
var textLen = $(this).val().length;
var textLeft = max - textLen;
. . .
My problem is that this code only runs in response to the numbers entered at the top of the keyboard, but does not run in response to numbers entered from the numeric keypad.
I\'m thinking the answer must be that the numeric keypad has different keyCode values, but how do I find out what those are?
回答1:
The keycodes are different. Keypad 0-9 is Keycode 96
to 105
Your if
statement should be:
if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
// 0-9 only
}
Here's a reference guide for keycodes
回答2:
******************* Don't use KEYCODE !!!! ******************
The problem with keyCode is to avoid the combined keys with the numbers on top of keyboard, we must add a check on the key "Shift" and "Alt" to avoid special characters such as e @ & " { } ...
A simplest solution is to convert e.key to a number and check if the conversion gives NaN!
let key = Number(e.key)
if (isNaN(key) || e.key===null) {
console.log("is not numeric")
}
else {
console.log("is numeric")
}
Be careful if e.key is null, it gives 0 !
Number(5) // => 5
Number('5') // => 5
Number(null) // => 0
Number('chars') // => NaN
Number(undefined) // => NaN
回答3:
You can simply run
$(document).keyup(function(e) {
console.log(e.keyCode);
});
to see the codes of pressed keys in the browser console.
Or you can find key codes here: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode#Numpad_keys
回答4:
keyCode is different for numbers on numeric keypad and numbers on top of keyboard.
keyCodes :
numbers on top of keyboard ( 0 - 9 ) : 48 - 57
numbers on numeric keypad ( 0 - 9 ) : 96 - 105
JavaScript condition :
if((e.keyCode >= 48 && e.keyCode <=57) || (e.keyCode >= 96 && e.keyCode <=105)) {
// entered key is a number
}
Reference for all keycodes ( with demo ) : http://www.codeforeach.com/javascript/keycode-for-each-key-and-usage-with-demo
回答5:
For the people that want a CTRL+C, CTRL-V solution, here you go:
/**
* Retrieves the number that was pressed on the keyboard.
*
* @param {Event} event The keypress event containing the keyCode.
* @returns {number|null} a number between 0-9 that was pressed. Returns null if there was no numeric key pressed.
*/
function getNumberFromKeyEvent(event) {
if (event.keyCode >= 96 && event.keyCode <= 105) {
return event.keyCode - 96;
} else if (event.keyCode >= 48 && event.keyCode <= 57) {
return event.keyCode - 48;
}
return null;
}
It uses the logic of the first answer.
回答6:
To add to some of the other answers, note that:
keyup
andkeydown
differ from keypress- if you want to use
String.fromCharCode()
to get the actual digit fromkeyup
, you'll need to first normalize thekeyCode
.
Below is a self-documenting example that determines if the key is numeric, along with which number it is (example uses the range function from lodash).
const isKeypad = range(96, 106).includes(keyCode);
const normalizedKeyCode = isKeypad ? keyCode - 48 : keyCode;
const isDigit = range(48, 58).includes(normalizedKeyCode);
const digit = String.fromCharCode(normalizedKeyCode);
回答7:
The answer by @.A. Morel I find to be the best easy to understand solution with a small footprint. Just wanted to add on top if you want a smaller code amount this solution which is a modification of Morel works well for not allowing letters of any sort including inputs notorious 'e' character.
function InputTypeNumberDissallowAllCharactersExceptNumeric() {
let key = Number(inputEvent.key);
return !isNaN(key);
}
回答8:
You can use the key code page in order to find the:
event.code
to diference the number keyboard.
https://keycode.info/
function getNumberFromKeyEvent(event) {
if (event.code.indexOf('Numpad') === 0) {
var number = parseInt(event.code.replace('Numpad', ''), 10);
if (number >= 0 && number <= 9) {
// numbers from numeric keyboard
}
}
}
回答9:
You can use this to figure out keyCodes easily:
$(document).keyup(function(e) {
// Displays the keycode of the last pressed key in the body
$(document.body).html(e.keyCode);
});
http://jsfiddle.net/vecvc4fr/
来源:https://stackoverflow.com/questions/13196945/keycode-values-for-numeric-keypad