前面的話
最近做Electron項目, 想要實現按Esc鍵來退出全屏窗口的功能。
需求如下:
按Esc鍵退出全屏窗口:
globalshortcut 模塊
Electron提供了全局快捷模塊(global shortcut module),允許開發者捕獲組合鍵並作出相應的反應。
-
引用
globalshortcut
模塊 -
在app.on(‘ready’, function() {})中註冊:
app.on('ready', createWindow);
function createWindow () {
/**
* Initial window options
*/
mainWindow = new BrowserWindow({
height: 563,
useContentSize: true,
width: 1000,
// frame:false
})
mainWindow.loadURL(winURL)
mainWindow.on('closed', () => {
mainWindow = null
})
event(mainWindow, app);
// 註冊esc,
globalShortcut.register('ESC', function () {
// 編寫你的代碼
// ...
})
}