How to disable “reopen last tab” in Microsoft Edge (Ctrl + Shift + T) using JavaScript?

We have a web application that defines a bunch of keyboard shortcuts. One of which is Ctrl + Shift + T. The problem is Microsoft Edge hooks in to this key combo to reopen the previously closed tab or window, and then switch to it.

Reference: Keyboard Shortcuts in Microsoft Edge.

I got playing around a little bit, trying to circumvent this using JavaScript:

<!DOCTYPE html>
<html>
<head>
    <title>Ctrl + Shift + T</title>
    <script type="text/javascript">
        var tKey = 84;

        function logKeys(event) {
            console.log(event.type + ": " + event.keyCode);

            if (event.keyCode === tKey) {
                event.preventDefault();
                event.stopPropagation();
                setTimeout(function() { window.focus(); }, 200);
            }
        }

        document.documentElement.addEventListener("keydown", logKeys);
        document.documentElement.addEventListener("keypress", logKeys);
        document.documentElement.addEventListener("keyup", logKeys);
    </script>
</head>
<body>
</body>
</html>

I was hoping to “cancel” the keyboard events using JavaScript so Edge does not reopen the previously open tab or window. The Edge keyboard shortcut is still taking precedence. In fact, the browser console does not even register a log message for the T key, as denoted by event.keyCode 84.

Maybe my JavaScript chops are getting a little rusty.

Is there a way to prevent Edge from reopening the previous tab or window when pressing Ctrl + Shift + T using JavaScript?

Answer

This is possible using the (currently experimental) Keyboard Lock API, which is available in Chrome and Edge (but not currently in IE, Firefox, or Safari):

Richly interactive web sites, games and remote desktop/application streaming experiences want to provide an immersive, full screen experience. To accomplish this, sites need access to special keys and keyboard shortcuts while they are in full screen mode so that they can be used for navigation, menus or gaming functionality. Some examples of the keys that may be required are Escape, Alt+Tab, Cmd+`, and Ctrl+N.

By default, these keys are not available to the web application because they are captured by the browser or the underlying operating system. The Keyboard Lock API enables websites to capture and use all available keys allowed by the OS.

Note that for usability reasons this API only works when your application has initiated full screen mode using the fullscreen API. You cannot interfere with standard browser keyboard shortcuts otherwise:

There are two different types of fullscreen available in modern user agents: JavaScript-initiated fullscreen (via the [Fullscreen] API) and user-initiated fullscreen (when the user enters fullscreen using a keyboard shortcut). The user-initiated fullscreen is often referred to as “F11” fullscreen since that is a common key shortcut used to enter and exit fullscreen mode.

F11 fullscreen and JavaScript (JS) fullscreen do not behave the same way. When a user enters F11 fullscreen, they can only exit it via the same keyboard shortcut that they used to enter it — the exitFullscreen() function will not work in this case. In addition, fullscreen events that are normally fired for JS fullscreen are not sent for F11 fullscreen.

Because of these differences (and because there is no standard shortcut for F11 fullscreen), the Keyboard Lock API is only valid when the a JavaScript-initiated fullscreen is active. During F11 fullscreen, no Keyboard Lock processing of keyboard events will take place.

So in your case, to capture Ctrl+Shift+T you would need to enter fullscreen mode using the Fullscreen API (you probably want to get the user’s consent for this first; entering fullscreen mode unexpectedly would be a bad user experience), then lock the T key using the keyboard lock API:

document.documentElement.requestFullscreen().then(() => {
  console.log("In fullscreen.");
  navigator.keyboard.lock(["KeyT"]).then(() => {
    console.log("Ctrl+Shift+T locked");
  });
})

More information: