键盘event.which属性

时间:2023-12-16 11:33:20

IE中,只有keyCode属性,而FireFox中有which和charCode属性

event.which属性对DOM原生的event.keyCode和event.charCode进行了标准化。

function keypress(e){
e = e || window.event;
var currKey = e.which || e.charCode || e.keyCode;var keyName = String.fromCharCode(currKey);
console.log("按键码: " + currKey + " 字符: " + keyName);
} document.onkeypress = function(e){
keypress(e);
}