将Javascript键代码转换为字符,反之亦然

将Javascript键代码转换为字符,反之亦然,javascript,keycode,Javascript,Keycode,我正在进行HTML输入过滤,我有以下情况: 我需要将keyCodes转换为char,但当我处理Numpad键时,会得到奇怪的结果: String.fromCharCode(96) //should be 0 but I recieve "`" String.fromCharCode(97) //should be 1 but I recieve "a" String.fromCharCode(98) //should be 2 but I receive "b" String.fromCharC

我正在进行HTML输入过滤,我有以下情况:

我需要将keyCodes转换为char,但当我处理Numpad键时,会得到奇怪的结果:

String.fromCharCode(96) //should be 0 but I recieve "`"
String.fromCharCode(97) //should be 1 but I recieve "a"
String.fromCharCode(98) //should be 2 but I receive "b"
String.fromCharCode(99) //should be 3 but I recieve "c"
String.fromCharCode(100) //should be 4 but I recieve "d"
String.fromCharCode(101) //should be 5 but I recieve "e"
String.fromCharCode(102) //should be 6 but I recieve "f"
String.fromCharCode(103) //should be 7 but I recieve "g"
从文档和事件调试中,我可以看到以下映射:

numpad 0    96
numpad 1    97
numpad 2    98
numpad 3    99
numpad 4    100
numpad 5    101
numpad 6    102
numpad 7    103 
链接:


我错过了什么?

可能只是一个愚蠢的错误。您应该看到数字的字符码是48-57

for ( let i = 0; i < 10; i++ ) {
  console.log( String.fromCharCode( 48 + i ) );
}
for(设i=0;i<10;i++){
log(String.fromCharCode(48+i));
}

请参阅man ascii使用该表获取字符代码。

好的,因此根据web上的几个来源,我应该执行以下逻辑:

if(keyCode >= 96 && keyCode <= 105){
    keyCode -= 48;
}

if(keyCode>=96&&keyCode检查此答案:请显示您正在使用的代码,这样我们可以更轻松地为您提供一个工作答案。什么最小、完整且可验证的示例?只需执行String.fromCharCode(103)你会得到一个例子,我的意思是你可能看到的ascii字符的数字是错误的。没有冒犯的意思:)。是的,谢谢:)我把键码和ascii码混淆了(因为String.fromCharCode文档没有提到这方面的任何内容)