Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron: How to prevent Cmd + Q from quitting?

I want to force the user only be allowed to quit the app from the Menu or Tray icon. I have those configured and working, but every time I hit Cmd+W or Cmd+Q it exits the app...

I tried catching it with:

app.on('quit', e => e.preventDefault())
app.on('window-all-closed', e => e.preventDefault())

...but it doesn't seem to have any effect.

like image 293
corysimmons Avatar asked Nov 05 '25 06:11

corysimmons


1 Answers

You can use Menu module to override the shortcut as below:

const { Menu, MenuItem } = require('electron')
const menu = new Menu()

menu.append(new MenuItem({
  label: 'Quit',
  accelerator: 'CmdOrCtrl+Q',
  click: () => { console.log('Cmd + Q is pressed') }
}));

It's up to you to hide or show this menu item because 'Quit' suppose to exit the application

like image 72
Amadou Beye Avatar answered Nov 08 '25 07:11

Amadou Beye