我想实现一个绑定到的自定义操作Command+R
电子应用程序中的键盘快捷键。
我克隆了electron-quick-start
回购,并改变了main.js
文件到此:
const { app, Menu, MenuItem, BrowserWindow } = require('electron')
let mainWindow
let template = [
{ label: app.getName(), submenu: [
{ label: 'custom action 1', accelerator: 'Command+R', click() { console.log('go!') } },
{ label: 'custom action 2', accelerator: 'Shift+Command+R', click() { console.log('go!') } },
{ type: 'separator' },
{ role: 'quit' }
] }
]
const menu = Menu.buildFromTemplate(template)
function createWindow () {
mainWindow = new BrowserWindow({width: 800, height: 600})
mainWindow.loadURL(`file://${__dirname}/index.html`)
mainWindow.webContents.openDevTools()
mainWindow.on('closed', function () { mainWindow = null })
// Set application menu
Menu.setApplicationMenu(menu)
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})
The menu works when the app is run npm start
. But when you press ⌘R the page reloads instead of performing the custom shortcut defined in the template.
我这里缺少什么吗?