常用的键盘事件

Tutorial: DOM实战 Category: JS Published: 2026-04-07 13:58:26 Views: 20 Likes: 0 Comments: 0
<!DOCTYPE html>
<html lang="en">
  <head></head>

  <body>
    <script>
      // 常用的键盘事件
      // 三个事件的执行顺序 keydown -- keypress -- keyup
      // 1. keyup 按键弹起的时候触发
      document.onkeyup = function () {
        console.log("我弹起了1");
      };

      document.addEventListener("keyup", function () {
        console.log("我弹起了2");
      });

      // 2. keypress 按键按下的时候触发  不能识别功能键 比如 ctrl shift 左右箭头啊
      document.addEventListener("keypress", function () {
        console.log("我按下了press");
      });

      //3. keydown 按键按下的时候触发  能识别功能键 比如 ctrl shift 左右箭头啊
      document.addEventListener("keydown", function () {
        console.log("我按下了down");
      });
    </script>
  </body>
</html>
Prev: 鼠标事件 Next: ASCII码