代码示例:(标识:event_key_key_if)
<!DOCTYPE html>
<html>
<body>

<p>在输入字段中按键盘上的 "A" 键来提示一些文本。</p>

<input type="text" size="40" onkeydown="myFunction(event)">

<script>
function myFunction(event) {
  var x = event.key;

  // 如果按下的键盘按钮是 "a" 或 "A"(使用大写锁定或移位),则提示一些文本。
  if (x == "a" || x == "A") { 
    alert ("You pressed the 'A' key!");
  }
}
</script>

</body>
</html>
运行结果: