无法阻止chrome浏览器上的快捷键

Cannot prevent keystroke shortcuts on chrome browser

本文关键字:快捷键 浏览器 chrome      更新时间:2023-09-26

我正在编写一个交互式应用程序,其中用户使用键盘执行操作。大多数击键都被正确捕获并执行,但在chrome上,当我按下(ctrl + t)时,会打开一个新的选项卡,而不是调用我的按键事件。它在Firefox和Internet Explorer上完美地工作,下面是示例代码

<html>
   <head>
      <style>
         #section {
         width: 80%;
         height: 500px;
         background: grey;
         margin: auto;
         }
      </style>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script>
         $(document).ready(function() {
           $("#section").keydown(function(e) {
             stopEvent(e);
             console.log(e.key + " down");
           });
           function stopEvent (e) {
             e.stopPropagation();
             e.preventDefault();
           }
         });
      </script>
   </head>
   <body>
      <div id="section" tabindex="1">
         <h2>Keyboard operations</h2>
      </div>
   </body>
</html>

看看这是否有帮助,

从:http://unixpapa.com/js/key.html

在keydown和keyup上,事件对象也有标志来指示键入键时按下了哪些修饰符键。这些是:

event.shiftKey
event.ctrlKey
event.altKey
event.metaKey