7

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;
    }
};
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Devin
  • 870
  • 9
  • 20
  • 3
    It's a browser feature - not something you can control at app level. – VLAZ Aug 08 '16 at 20:52
  • 2
    But for instance the web app I'm using overrides Ctrl+S to save inside the web app instead of the raw HTML to disk. Is there no way to do this for Ctr+N? – Devin Aug 08 '16 at 20:53
  • 2
    See this question: http://stackoverflow.com/questions/15911785/overriding-shortcut-keys-in-firefox-and-chrome – NullUserException Aug 08 '16 at 20:57
  • @Vld The real answer is _many_ browser shortcuts can be intercepted. Unfotunately Ctrl+N is not one of them... for Chrome at least. You can change your app so it uses a different shortcut – NullUserException Aug 08 '16 at 20:58
  • @NullUserException - indeed I was just feeling terse. You can (try to) capture Ctrl + N but you can't prevent the default behaviour. With a very good reason, to be honest - if a website to deny my the default shortcuts, I'd pre pretty annoyed. – VLAZ Aug 08 '16 at 21:00
  • @Vld Sometimes those shortcuts can be useful. For example when you are using Google Docs you want Ctrl + F to launch the app's find function rather than the browser's, because what you are trying to find is not yet rendered. I think the only shortcut I would not want to be messed with would be Ctrl + W because of potential for abuse. – NullUserException Aug 08 '16 at 21:03
  • @NullUserException Ctrl + W, Ctrl + T, Ctrl + N, Ctrl + R, F5, Ctrl + Shift + N (for Chrome, or Ctrl + Shift + P in Firefox), back, forward - there are a lot I'd rather keep to myself. Things like PgUp/Down or Ctrl + F could be overwritten but only if they provide pretty similar functionality. Yet, even then I've found apps that provide search to sometimes not be as useful as the browser find - for example, if I want to search case sensitive (not available in Chrome...) or if I want to highlight all occurrences. The better apps defer to the browser functionality if I press Ctrl + F twice. – VLAZ Aug 08 '16 at 21:15
  • Unfortunately it's not my app, I'm just a user trying to run some JavaScript in the browser to streamline some things. So I can't change the app itself, but I thought I could maybe simply fire off a keyboard event when pressing Ctrl+Q for example, that way there would be no chance of accidentally typing Ctrl+N and ruining my flow. Unfortunately, all this code does it insert a capital M, it seems to ignore the Ctrl portion of it. Any ideas? $(document).trigger({type:'keypress', which: 77, keyCode: 77, ctrlKey: true }); – Devin Aug 08 '16 at 21:31
  • You could make it an electron application. – Lime Aug 13 '18 at 01:18

3 Answers3

18

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

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Theo
  • 249
  • 1
  • 6
  • 1
    Welcome to Stack Overflow! Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Enamul Hassan Aug 09 '16 at 00:02
0

You can find a github issue by the GateOne project discussing the same problem here.

They say the only workaround is to "Create an application shortcut" where you can now override the Ctrl+N key.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Ben Fogel
  • 541
  • 5
  • 13
-1

Under Linux one can disable the combination by redefining the keyboard mapping at the Xorg level. Then it will be disabled for all applications running under X in the current session. It is often useful to disable various control combinations in a kiosk-type app running in fullscreen mode. For example, your .xinitrc could set the custom keyboard mapping:

#!/usr/bin/env bash
        
test -f ~/.Xkeymap && xkbcomp ~/.Xkeymap $DISPLAY &
        
while :; do
/usr/lib/chromium-55.0.2866.0-16k/chrome-wrapper \
--disable-seccomp-filter-sandbox --user-data-dir=.chromium-55.0.2866.0-16k \
--app="http://1.1.1.1:10001/terminal/start" --start-fullscreen \
--kiosk --window-position=0,0 --window-size=1920,1080 
done

where your modified .Xkeymap would remap <LatN> key to something harmless like <F3> when used in combination with <Ctrl> and other modifier keys:

    type "MYCONTROL" {
        modifiers= Shift+Lock+Control+Alt+LevelThree;
        map[Shift]= Level2;
        map[Lock]= Level2;
        map[Alt]= Level3;
        map[LevelThree]= Level4;
        map[Shift+LevelThree]= Level5;
        map[Control+Alt]= Level6;
        map[Control]= Level7;
        map[Shift+Control]= Level8;
        level_name[Level1]= "Base";
        level_name[Level2]= "Caps";
        level_name[Level3]= "Alt Base";
        level_name[Level4]= "L3 Base";
        level_name[Level5]= "Shift L3";
        level_name[Level6]= "Ctrl+Alt";
        level_name[Level7]= "Ctrl Base";
        level_name[Level8]= "Shift+Ctrl";
    };

...

    key <LatN> {
       type= "MYCONTROL",
       symbols[Group1]= [ n, N, F3, F3, F3, F3, F3, F3 ]
    };
xxor
  • 94
  • 3