electron 快捷键comman+w关闭窗口

使用 向操作系统注册/注销全局键盘快捷键

const{ app, globalShortcut }=require('electron')
app.whenReady().then(()=>{let ret = globalShortcut.register('CommandOrControl+W',()=>{// 关闭窗口
	mainWindow.close()// 关闭当前聚焦窗口// BrowserWindow.getFocusedWindow()})if(!ret){
    console.log('registration failed')}// Check whether a shortcut is registered.
  console.log(globalShortcut.isRegistered('CommandOrControl+W'))})W
app.on('will-quit',()=>{// Unregister a shortcut.
  globalShortcut.unregister('CommandOrControl+X')// Unregister all shortcuts.
  globalShortcut.unregisterAll()})

为了避免焦点移动到其他软件上,使用注册的快捷键发生错误

app.on('browser-window-focus',()=>{
  globalShortcut.register('CommandOrControl+W')})
app.on('browser-window-focus',()=>{// Unregister a shortcut.
  globalShortcut.unregister('CommandOrControl+X')// Unregister all shortcuts.W'
  globalShortcut.unregisterAll()})