【问题标题】:Electron.js multiple acceleratorsElectron.js 多个加速器
【发布时间】:2019-07-17 17:22:21
【问题描述】:

让我们看看 Electron 菜单模板的这段代码:

const menu = [
  {
    label: "foo"
    submenu: [
      {
        label: "bar",
        accelerator: "Control+B"
      {
    ]
  }
]

如何为同一个菜单项注册多个快捷键?

真实示例:我想同时注册 F3Control+F 以使用在页面中查找功能。

【问题讨论】:

    标签: electron


    【解决方案1】:

    至少在 Electron 的更高版本中解决此问题的一种方法是添加一个额外的不可见菜单项,具有相同的功能但不同的加速器:

    const menu = [
      {
        label: "foo"
        submenu: [
          {
            label: "bar",
            accelerator: "Control+B"
          },
          {
            label: "bar (invisible)",
            accelerator: "Control+C",
            visible: false,   
            acceleratorWorksWhenHidden: true
          },
        ]
      }
    ]
    

    这将完全一样,但不显示,acceleratorWorksWhenHidden 仍然会收听快捷方式。

    【讨论】:

      【解决方案2】:

      到目前为止(Electron 5.0.7)的不幸答案是您不能(本机)。这是一个被跟踪的issue

      一位评论者提出了这种解决方法:

      // it's not possible to add multiple accelerators
      // so need to do this the oldschool way
      document.addEventListener('keydown', event => {
          if (process.platform === 'darwin' && event.metaKey && event.shiftKey) {
              if (event.keyCode === 221/* ] */) {
                  nextConversation();
              }
      
            if (event.keyCode === 219/* [ */) {
                  previousConversation();
              }
          }
      });
      

      其他人建议使用electron-localshortcut 模块来解决此问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-05
        • 1970-01-01
        • 2016-04-27
        • 1970-01-01
        • 2018-11-20
        • 1970-01-01
        • 1970-01-01
        • 2017-02-26
        相关资源
        最近更新 更多