如果没有提供浏览器窗口,电子本地快捷方式无法正常工作

时间:2017-05-27 14:13:42

标签: javascript electron

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是一个必要的参数,但在链接中,可以注册该快捷方式而不给出任何窗口。

1 个答案:

答案 0 :(得分:0)

选项1

在所有窗口上注册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());
}

选项2

添加一个不可见的菜单项,然后将accelerator选项设置为'CmdOrCtrl+Alt+Super+Right',如下所示:

{
    accelerator: 'CmdOrCtrl+Alt+Super+Right'
    visible:     false,
    click:       function () {
        shortcutPressed();
    }
}

(这在OSX上不起作用,因为它需要可见才能使快捷方式起作用)