Loki
Loki

Reputation: 1205

Electron Global shortcut find browser instance

Hi guys I'm using electron-globalshortcut

I would like to override the default shortcut 'CTRL+W' for browser windows since I have a few of them like popups.

The problem that i'm having is that when I define global shortcut like this:

 globalShortcut.register('CommandOrControl+W', () => {
    console.log('CommandOrControl+W is pressed')
  })

How do I get on what Browser instance it is? How could I get a reference to correct Browser window

Upvotes: 0

Views: 301

Answers (1)

Luciano
Luciano

Reputation: 21

When you create your BrowserWindow, you do it like this

const window = new BrowserWindow({.../*Your options here*/});

So, you can bind the register to this window variable:

window.on("focus", () => {
globalShortcut.register('CommandOrControl+W', () => {
    console.log('CommandOrControl+W is pressed');
  })
});

window.on("blur", () => {
globalShortcut.unregisterAll()
});

Also, it is recommended to call the registers inside ready app event. And if you have more than one BrowserWindow, the current focused one can be detected with

BrowserWindow.getFocusedWindow()

Please refer to https://www.electronjs.org/docs/api/browser-window

Upvotes: 1

Related Questions