-1

I am writing an interactive application where user performs operations using keyboard. Most of the keystrokes are captured properly and executed, but on chrome when I press (ctrl + t) a new tab opens up, instead of calling my keypress event. Where as it works perfectly on Firefox and Internet Explorer, here is the sample code

<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>
johncorner06
  • 97
  • 1
  • 7
  • That's up to the Browser vendor. If it falls withing the scope of actual computer usage they should not prevent it with JavaScript. – StackSlave Oct 01 '16 at 00:19
  • The fact IE & Firefox allow this, is very surprising. preventing ctrl-t I wound't expect to be able to prevent. – Keith Oct 01 '16 at 00:20
  • I agree. I guess they figure you can still move your mouse if you want a new tab. – StackSlave Oct 01 '16 at 00:22
  • 1
    Check this [http://stackoverflow.com/questions/7295508/javascript-capture-browser-shortcuts-ctrlt-n-w/7296303#7296303](http://stackoverflow.com/questions/7295508/javascript-capture-browser-shortcuts-ctrlt-n-w/7296303#7296303) – Rohit Oct 01 '16 at 00:24

1 Answers1

0

See if this is of any help,

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

On keydown and keyup, the event objects also have flags that indicate which modifier keys were being pressed when the key was typed. These are:

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

RLoniello
  • 2,309
  • 2
  • 19
  • 26