1

I'm making an electron desktop app. I want to disable windows key and function keys while the app is on

I tried using the following code ... it registers the event but the windows menu opens anyways

$(document).keydown(function(e){

    if (e.keyCode == 37) { 
       alert( "windows key pressed" );
       return false;
    }
});

Any help?

  • To put it simply, you can't. [More information](https://github.com/electron/electron/issues/1395) – Ben Fortune Jun 01 '17 at 13:12
  • That's an issue from 2015. Check https://github.com/electron/electron/blob/master/docs/api/global-shortcut.md They added the 'Super' Accelerator for Windows – VladNeacsu Jun 01 '17 at 13:22

1 Answers1

1

You can try this, but unforunately it will become a global shortcut, meaning when the window doesn't have focus it will still be registered. Try putting a console.log() to see when it fires. win is your electron window variable

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

win = new BrowserWindow();

globalShortcut.register('Super', () => {
  if (win.isFocused()) {
    // do something
  }
});

You can check the docs here: docs

Or try to use this module here: electron-localshortcut

electronLocalshortcut.register(win, 'Super', () => {
    console.log('Windows Button pressed');
    return false;
});
VladNeacsu
  • 1,268
  • 16
  • 33
  • I put the code in my main.js ... this works ... but when i use 'Super' instead of 'CTrl+A' the app breaks ... electronLocalshortcut.register(mainWindow, 'Ctrl+A', () => { console.log('You pressed ctrl & A'); }); – Sushil Sudhakaran Jun 01 '17 at 15:04
  • What's the Error? – VladNeacsu Jun 02 '17 at 11:11
  • 1
    @VladNeacsu I know this is old, but I believe the binding function can not register singular expressions like `Super`, `Ctrl` and etc (has to be `Super+Something`, `ctrl+a` and not just `ctrl` or `super`). Do you have any other conceptions? – undefined Oct 07 '18 at 07:02
  • See the github issue regarding this: https://github.com/electron/electron/issues/9206 Basically shortcuts that windows already uses (Super, Super+V) cant be used. The globalShortcut.register-function will also return a boolean indicating if it was successful. To implement this anyway use othery libraries like the one stated in the issue. – Philip Müller Nov 01 '20 at 14:02