Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Capturing <kbd>Ctrl</kbd> keyboard events in Javascript</h1> <h3>Sample code:</h3> <pre><code>$(window).keydown(function(event) { if(event.ctrlKey &amp;&amp; event.keyCode == 84) { console.log("Hey! Ctrl+T event captured!"); event.preventDefault(); } if(event.ctrlKey &amp;&amp; event.keyCode == 83) { console.log("Hey! Ctrl+S event captured!"); event.preventDefault(); } }); </code></pre> <hr> <h2>Firefox</h2> <p><sub>(6.0.1 tested)</sub></p> <p>In Firefox both event listener works. If you press <kbd>Ctrl</kbd><kbd>T</kbd> or <kbd>Ctrl</kbd><kbd>S</kbd> keycombinations, you will get both message on the console, and the browser wont open a tab, nor ask for save.</p> <p><em>It is intresting that if you use <code>alert</code> instead of <code>console.log</code> the <code>event.preventDefault()</code> not works, and opens a new tab or asks for save. Maybe this bug needs to get fixed.</em></p> <hr> <h2>Chrome3</h2> <p>In Chrome 3 it works like in Firefox.</p> <hr> <h2>Chrome4</h2> <p><sub>(tested)</sub></p> <blockquote> <p>In Chrome4, certain control key combinations have been reserved for browser usage only and can no longer be intercepted by the client side JavaScript in the web page.<br> These restrictions did not exist in Chrome3 and are inconsistent with both Firefox3/3.5 and IE7/8 (on Windows).</p> </blockquote> <p>In Chrome 4 it works similary to Firefox, except some keyboard combination:</p> <ul> <li><p><kbd>Ctrl</kbd><kbd>N</kbd></p></li> <li><p><kbd>Ctrl</kbd><kbd>Shift</kbd><kbd>N</kbd></p></li> <li><p><kbd>Ctrl</kbd><kbd>T</kbd></p></li> <li><p><kbd>Ctrl</kbd><kbd>Shift</kbd><kbd>T</kbd></p></li> <li><p><kbd>Ctrl</kbd><kbd>W</kbd></p></li> <li><p><kbd>Ctrl</kbd><kbd>Shift</kbd><kbd>W</kbd></p></li> </ul> <p>These combinations cannot get captured by Javascript, <strong>but</strong> embed plugins can capture these. For example if you focus in a Youtube video and press <kbd>Ctrl</kbd><kbd>T</kbd>, the browser won't open a new tab.</p> <hr> <h2>IE7/8</h2> <p>It works like in Firefox or Chrome3.</p> <hr> <h2>IE9</h2> <p><sub>(tested)</sub></p> <p>IE9 is a black sheep again, because it dosen't allow javascript to capture any <kbd>Ctrl</kbd><kbd>?</kbd> keyboard event. I tested with many keyboard combination (R,T,P,S,N,T) and neither worked. Also embed applications can't capture the event. Tested with Youtube videos.</p> <hr> <p><strong>Thanks to <a href="https://stackoverflow.com/questions/7295508/#comment-8789103">@Lime</a> for the great <a href="http://code.google.com/p/chromium/issues/detail?id=33056" rel="noreferrer">link</a>.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload