【问题标题】:Disable all keyboard shortcuts in Electron.js禁用 Electron.js 中的所有键盘快捷键
【发布时间】:2021-11-19 17:01:18
【问题描述】:

我正在尝试在我的 electron.js 应用中禁用所有键盘快捷键

我尝试了以下方法(剧透警告:它们不起作用):

globalShortcut.unregisterAll()

globalShortcut.register('Alt+CommandOrControl+A', () => {
    console.log('not allowed')
  })
  globalShortcut.register('Alt+CommandOrControl+B', () => {
    console.log('not allowed')
  })
  globalShortcut.register('Alt+CommandOrControl+C', () => {
    console.log('not allowed')
  })
  globalShortcut.register('Alt+CommandOrControl+D', () => {
    console.log('not allowed') // and so on
  })

(我为所有键(从 A 到 Z、1 到 9 等)做了这个 ^。 顺便说一句,我尝试的所有代码都放入了app.whenReady() 函数中。

好吧,这些都不起作用。我看到了很多其他更抽象方式的文章,但它们也没有奏效。 我实际上也尝试过搜索 npm 包,但我没有找到任何可以解决我的问题的包。

我只需要从我的电子应用程序中完全禁用所有键盘快捷键。还有其他方法(实际上可行)吗?

【问题讨论】:

  • 不确定是否是最佳解决方案,但如果您在 alt 键为真时拦截 before-input-event 和 preventDefault 会怎样
  • 我该怎么做?你能写一个包含所有细节的答案吗?
  • @pushkin 所以你能写一个更详细的答案吗?

标签: node.js electron keyboard-shortcuts


【解决方案1】:

这里是简单的解决方案::

app.on('browser-window-focus', function () {
    globalShortcut.register("CommandOrControl+R", () => {
        console.log("CommandOrControl+R is pressed: Shortcut Disabled");
    });
    globalShortcut.register("F5", () => {
        console.log("F5 is pressed: Shortcut Disabled");
    });
});
app.on('browser-window-blur', function () {
    globalShortcut.unregister('CommandOrControl+R');
    globalShortcut.unregister('F5');
});

您可以通过在窗口聚焦时注册和在模糊时取消注册来禁用所有快捷方式。

在这里看到这个问题Disable reload via keyboard shortcut electron app

编辑:禁用所有快捷方式globalShortcut.registerAll(accelerators, callback) Accelerators String[] - 一个加速器数组。 回调函数 注册加速器中所有加速器项的全局快捷方式。当用户按下任何注册的快捷方式时调用回调。

globalShortcut.unregisterAll() 取消注册所有全局快捷方式。

【讨论】:

  • 谢谢!这是有效的!但是有什么方法可以对所有密钥批量执行此操作吗?所以我不必手动操作...
  • 没有特定的单一功能或方法可以禁用所有快捷方式。但是您可以像禁用单个快捷方式一样使用 registerAll 和 unregisterAll 方法。
  • 我试过了,它不起作用...我想我只是将你的方法用于所有快捷方式
猜你喜欢
  • 1970-01-01
  • 2012-11-06
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多