<p>在输入框中输入键盘上的字母获取按下字母的 Unicode 字母代码和 Unicode 键代码 。</p> <p><strong>注意:</strong> which 和 keyCode 属性不能用于onkeypress的非打印事件,功能键 (如 CAPS LOCK, CTRL, ESC, F12, 等)。</p> <input type="text" size="50" onkeypress="uniCharCode(event)" onkeydown="uniKeyCode(event)"> <p>onkeypress - <span id="demo"></span></p> <p>onkeydown - <span id="demo2"></span></p> <script> function uniCharCode(event) { var char = event.which || event.keyCode; document.getElementById("demo").innerHTML = " Unicode 字符代码为: " + char; } function uniKeyCode(event) { var key = event.which || event.keyCode; document.getElementById("demo2").innerHTML = " Unicode 键代码为: " + key; } </script>