KeyboardEvent的哪个属性用于返回触发onkeypress事件的键的Unicode字符代码。它还返回触发onkeydown或onkeyup事件的 key 的Unicode key 代码。键码是一个数字,代表键盘上的实际键,与代表ASCII字符的字符码不同。
用法:
event.which
以下示例程序旨在说明KeyboardEvent的哪个属性:
例:返回 key 的Unicode字符代码。
KeyboardEvent which Property in HTMLh1 {
color:green;
}
h2 {
font-family:Impact;
}
body {
text-align:center;
}
GeeksforGeeks
KeyboardEvent which Property
To get the Unicode character code of the key,
Press the key on the keyboard in the input field.
function MyEvent(event) {
var e = event.which;
document.getElementById("test").innerHTML =
"Unicode value of the pressed key is:" + e;
}
输出:
单击按钮后
支持的浏览器:
Opera
IE浏览器
谷歌浏览器
Firefox
苹果Safari