I use a web app for work, and one of the shortcuts I use often is Ctrl+M. However, I'm often typing very quickly and mistakenly hit Ctrl+N by mistake, and instead of triggering an action in my web app, I open up a new window. I would like to be able to prevent that, but I can't seem to figure out how. So far I've tried running this code every time the page loads, but it doesn't seem to do anything. (Ignore the fact that I'm using a switch for just one key, I have a few other custom Ctrl shortcuts that don't override Chrome shortcuts that are working, I just removed them for readability)
document.onkeydown = function(e)
{
if(e.ctrlKey === false)
{
return;
}
switch(e.which) {
case 78:
//just to keep from opening a new tab when merging cells
e.preventDefault();
e.stopPropagation();
break;
default:
return;
}
};
There is no way to override Ctrl+N, Ctrl+T, or Ctrl+W in Google Chrome since version 4 of Chrome (shipped in 2010).
As stated on the Chromium issue tracker:
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.
Only known workaround is to open your webpage/extension as a Chrome app where it will again have permission to override these blacklisted key combos
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With