electon-localshortcut
在尝试分配快捷方式而不指定https://www.npmjs.com/package/electron-localshortcut中提供的任何浏览器窗口时发出错误
electronLocalshortcut.register('CmdOrCtrl+Alt+Super+Right', () => {
console.log('move captured')
})
以下是显示的错误:
Uncaught Exception:
TypeError: win.on is not a function
at register (/home/user/myapp/node_modules/electron-localshortcut/index.js:68:7)
at Object.register (/home/user/myapp/node_modules/electron-localshortcut/index.js:51:3)
at setAppLevelShortcuts (/home/user/myapp/main.js:65:24)
at App.startUp (/home/user/myapp/main.js:39:2)
at emitTwo (events.js:111:20)
at App.emit (events.js:191:7)
似乎window
是一个必要的参数,但在链接中,可以注册该快捷方式而不给出任何窗口。
答案 0 :(得分:0)
在所有窗口上注册electron-localshortcut
然后按下它然后运行一个检查焦点窗口的函数,如下所示:
var win1 = new BrowserWindow({title: "Win 1"});
var win2 = new BrowserWindow({title: "Win 2"});
electronLocalshortcut.register(win1, 'CmdOrCtrl+Alt+Super+Right', () => {
shortcutPressed();
});
electronLocalshortcut.register(win2, 'CmdOrCtrl+Alt+Super+Right', () => {
shortcutPressed();
});
function shortcutPressed () {
var focusedWin = BrowserWindow.getFocusedWindow();
console.log("Shortcut pressed on " + focusedWin.getTitle());
}
添加一个不可见的菜单项,然后将accelerator
选项设置为'CmdOrCtrl+Alt+Super+Right'
,如下所示:
{
accelerator: 'CmdOrCtrl+Alt+Super+Right'
visible: false,
click: function () {
shortcutPressed();
}
}
(这在OSX上不起作用,因为它需要可见才能使快捷方式起作用)