我正在开发一个网站,为了做一些安全措施,我需要检测一些 按键盘键时按键代码。任何人请帮我解决这个问题,使用java脚本或任何其他方法......
提前致谢..
答案 0 :(得分:4)
您可以尝试使用:
window.addEventListener('keydown', onKeyDown, true);
现在你可以使用一个名为onKeyDown(evt)的函数来查看按下了哪个键:
function onKeyDown(event) {
switch (event.keyCode) {
case 38: /* Up arrow was pressed */
//Do something
break;
case 40: /* Down arrow was pressed */
//Do something
break;
case 37: /* Left arrow was pressed */
//Do something
break;
case 39: /* Right arrow was pressed */
break;
}
}
我希望这会有所帮助:D
答案 1 :(得分:1)
获取密钥代码的示例
<script> function handleEvent(oEvent) {
var oTextbox = document.getElementById("txt1");
oTextbox.value += "\n>" + oEvent.type;
oTextbox.value += "\n target is " +
(oEvent.target || Event.srcElement).id;
oTextbox.value += "\n keyCode is " + oEvent.keyCode;}
</script>
<body>
<P>Type some characters into the first textbox.</p>
<P><textarea id="txtInput" rows="15" cols="50"
onkeypress="handleEvent(event)"></textarea></p><P><textarea id="txt1" rows="15" cols="50"></textarea></p>
</body>
答案 2 :(得分:0)