// 注册onKeyDown事件
var pressedKey;
var keyInfo;
window.onload = function() {
document.getElementById("sql").onkeydown = register;
}
function register(e) {
if (!e) {
e = window.event;
}
if (document.all){
pressedKey = e.keyCode;
} else{
pressedKey = e.which;
}
keyInfo = String.fromCharCode(pressedKey).toLowerCase()
+ "/npressedKey:/t" + pressedKey
+ "/nshiftKey:/t" + e.shiftKey
+ "/nctrlKey:/t" + e.ctrlKey
+ "/naltKey:/t" + e.altKey;
alert(keyInfo);
}
<div id="sql" onkeydown="test(event)" contenteditable="true" style="width: 300px; height: 100px; border-width:1px; border-color:#A7A6AA; border-style:solid; font-style: italic; font-weight: normal; color:#A7A6AA;">
asdf<font color='red'>fdfdfd</font><br/>kjli
</div>
function test(event) {
alert(event.which);
}
本文介绍了一种在网页中使用JavaScript来监听和处理键盘事件的方法。通过实例代码展示了如何获取按键信息,包括按键字符、按键码及组合键状态等。这对于开发需要键盘输入交互的应用非常有用。
617

被折叠的 条评论
为什么被折叠?



