要检查按下了哪个键,请使用onkeydown属性。
您可以尝试运行以下代码来获取按下哪个键:
<!DOCTYPE html> <html> <body> <p>Press a key in the below box to get which key is pressed.</p> <input type="text" size="10" onkeydown="demoFunction(event)"> <p id="test"></p> <script> function demoFunction(event) { var a = event.key; document.getElementById("test").innerHTML = "You've pressed the key: " + a; } </script> </body> </html>