7

I've been scouring the interwebz and Atom-shell documentation trying to find out how to disable the back() functionality of the backspace key within a browser window.

I would prefer not to have to resort to a javascript onkeydown listener (which works) and rather use something more native and at more of the application level instead of the browser window level.

1 Answer 1

3

The only way I have figured out to do this without the onkeydown listener is with a global-shortcut and the ipc events in the Electron api.

First a disclaimer...

Disabling any key with a global shortcut really does disable it GLOBALLY on your computer! PLEASE BE CAREFUL WHEN USING GLOBAL SHORTCUTS! If you forget to unregister your shortcut, or do not handle it properly, you will find it difficult to fix your mistake without backspace!

That said this is what worked for me...

const { app, ipcMain,
    globalShortcut,
    BrowserWindow,
} = require('electron');

app.on('ready', () => {

    // Create the browser window
    let mainWindow = new BrowserWindow({width: 800, height: 600});

    // and load the index.html of the app
    mainWindow.loadUrl('file://' + __dirname + '/index.html');

    // Register a 'Backspace' shortcut listener when focused on window
    mainWindow.on('focus', () => {

        if (mainWindow.isFocused()) {
            globalShortcut.register('Backspace', () => {

                // Provide feedback or logging here 
                // If you leave this section blank, you will get no
                // response when you try the shortcut (i.e. Backspace).

                console.log('Backspace was pressed!'); //comment-out or delete when ready.
            });
        });
    });

    //  ** THE IMPORTANT PART **
    // Unregister a 'Backspace' shortcut listener when leaving window.
    mainWindow.on('blur', () => {

        globalShortcut.unregister('Backspace');
        console.log('Backspace is unregistered!'); //comment-out or delete when ready.
    });
});

Alternatively you could add the shortcut inside an ipc "Toggle" event handler like this...

// In the main process
ipcMain.on('disableKey-toggle', (event, keyToDisable) => {
    if (!globalShortcut.isRegistered(keyToDisable){

        globalShortcut.register(keyToDisable, () => {
            console.log(keyToDisable+' is registered!'); //comment-out or delete when ready.

        });
    } else {

        globalShortcut.unregister(keyToDisable);
        console.log(keyToDisable+' is unregistered!'); //comment-out or delete when ready.
    }
});

// In the render process send the accelerator of the keyToDisable.
// Here we use the 'Backspace' accelerator.
const { ipcRenderer } = require('electron');
ipcRenderer.send('disableKey-toggle', 'Backspace'); 
2
  • Why would you block the Backspace key for the entire application? You can just block it in your frontend / renderer with "normal" javascript, described here on stackoverflow (or a similar answer below that answer). :) I know the topic starter asked for the nodejs way... still don't get why - people should just keep this way in mind... (: Commented Jul 4, 2015 at 23:09
  • 1
    I agree, that is usually the easiest way. However, this question asked how to do it using Electron and not Javascript. I imagine a more applicable use case would be: if you open a window a different application or a system dialogue where you want to continue to intercept (not block) the key-press.
    – Josh
    Commented Jul 4, 2015 at 23:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.