数字键盘(10键)字符代码不正确

时间:2017-05-16 21:47:21

标签: javascript jquery fromcharcode numpad

我注意到当我收听按键时,Chrome和IE中的字符代码会有所不同。有谁知道为什么这是在讨价还价?

铬:

numpad key: 0, String.fromCharCode(48) => "0"
numpad key: 1, String.fromCharCode(49) => "1"
numpad key: 2, String.fromCharCode(50) => "2"
numpad key: 3, String.fromCharCode(51) => "3"
numpad key: 4, String.fromCharCode(52) => "4"
numpad key: 5, String.fromCharCode(53) => "5"
numpad key: 6, String.fromCharCode(54) => "6"
numpad key: 7, String.fromCharCode(55) => "7"
numpad key: 8, String.fromCharCode(56) => "8"
numpad key: 9, String.fromCharCode(57) => "9"

IE:

numpad key: 0, String.fromCharCode(96)  => "`"
numpad key: 1, String.fromCharCode(97)  => "a"
numpad key: 2, String.fromCharCode(98)  => "b"
numpad key: 3, String.fromCharCode(99)  => "c"
numpad key: 4, String.fromCharCode(100) => "d"
numpad key: 5, String.fromCharCode(101) => "e"
numpad key: 6, String.fromCharCode(102) => "f"
numpad key: 7, String.fromCharCode(103) => "g"
numpad key: 8, String.fromCharCode(104) => "h"
numpad key: 9, String.fromCharCode(105) => "i"

我使用jquery which应规范化event.keyCodeevent.charCode

$("#input").keypress(function(e){
    console.log e.which, String.fromCharCode(e.which)
})

这与this answer不同,因为它使用的是vanilla javascript e.keyCode而不是jquery e.which

我想知道这是否与使用Windows虚拟机(Parallels Desktop)有关。

0 个答案:

没有答案