1

I am trying to disable all keyboard shortcuts in my electron.js app.

I tried the following ways (spoiler alert: they didn't work):

globalShortcut.unregisterAll()

and

globalShortcut.register('Alt+CommandOrControl+A', () => {
    console.log('not allowed')
  })
  globalShortcut.register('Alt+CommandOrControl+B', () => {
    console.log('not allowed')
  })
  globalShortcut.register('Alt+CommandOrControl+C', () => {
    console.log('not allowed')
  })
  globalShortcut.register('Alt+CommandOrControl+D', () => {
    console.log('not allowed') // and so on
  })

(i did this ^ for all the keys (from A to Z, 1 to 9, etc). By the way, all of the code I tried I put into the app.whenReady() function.

Well, none of this worked. I saw a lot of articles with other more abstract ways, but they didn't work either. I actually tried searching for an npm package too, but I didn't find any that would solve my problem.

I just need to completely disable all keyboard shortcuts from my electron app. Is there any other way (that actually works)?

SCRACX
  • 11
  • 4
  • not sure if best solution but what if you intercept [before-input-event](https://www.electronjs.org/docs/api/web-contents#event-before-input-event) and preventDefault when the alt key is true – pushkin Sep 27 '21 at 15:25
  • How would i do that? Can you write an answer with all the details? – SCRACX Sep 27 '21 at 16:18
  • @pushkin so can you please write an answer with more details? – SCRACX Sep 28 '21 at 13:18

1 Answers1

1

Here is simple solution ::

app.on('browser-window-focus', function () {
    globalShortcut.register("CommandOrControl+R", () => {
        console.log("CommandOrControl+R is pressed: Shortcut Disabled");
    });
    globalShortcut.register("F5", () => {
        console.log("F5 is pressed: Shortcut Disabled");
    });
});
app.on('browser-window-blur', function () {
    globalShortcut.unregister('CommandOrControl+R');
    globalShortcut.unregister('F5');
});

You can disable all the shortcuts by registering while the window is focused and unregister when blur.

See this question right here Disable reload via keyboard shortcut electron app

EDIT : disable all shortcuts globalShortcut.registerAll(accelerators, callback) accelerators String[] - an array of Accelerators. callback Function Registers a global shortcut of all accelerator items in accelerators. The callback is called when any of the registered shortcuts are pressed by the user.

globalShortcut.unregisterAll() Unregisters all of the global shortcuts.

  • Thank you! This is working! But is there any way of doing this for all keys bulk? So I don't have to do it manually... – SCRACX Oct 02 '21 at 11:15
  • There is not a specific single function or method to disable all the shortcuts. But you can use the registerAll and unregisterAll methods the same way as disabling single shortcut. – Muhammad Taif Khan Oct 02 '21 at 13:21
  • I tried it and it doesn't work... I guess ill just use your method for all shortcuts – SCRACX Oct 04 '21 at 11:12