Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to override Ctrl+N to open a new window in Chrome?

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;
    }
};
like image 816
Devin Avatar asked Dec 07 '22 21:12

Devin


1 Answers

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

like image 77
Theo Avatar answered Dec 10 '22 09:12

Theo