键盘快捷键

    示例

    应用键盘快捷键仅在应用程序被聚焦时触发。 To configure a local keyboard shortcut, you need to specify an [] property when creating a within the Menu module.

    从 中的应用开始,将以下内容更新到 main.js

    启动 Electron 应用程序后,你应该看到应用程序菜单以及您刚刚定义的本地快捷方式:

    If you click Help or press the defined accelerator and then open the terminal that you ran your Electron application from, you will see the message that was generated after triggering the click event: “Electron rocks!”.

    要配置全局键盘快捷键, 您需要使用 globalShortcon 模块来检测键盘事件,即使应用程序没有获得键盘焦点。

    从 中的应用开始,将以下内容更新到 。

    NOTE: In the code above, the CommandOrControl combination uses Command on macOS and Control on Windows/Linux.

    如果您想要在 BrowserWindow 中处理键盘快捷键,你可以在渲染进程中使用 来监听 kepupkeydown DOM事件

    Note the third parameter indicates that the listener will always receive key presses before other listeners so they can’t have stopPropagation() called on them.

    拦截主进程中的事件

    在调度页面中的keydownkeyup事件之前,会发出事件。 它可以用于捕获和处理在菜单中不可见的自定义快捷方式。

    Quick Start Guide 中的应用开始,将以下内容更新到 main.js

    使用第三方库

    如果您不想手动进行快捷键解析,可以使用一些库来进行高级的按键检测。例如 . 以下是在渲染进程中 mousetrap 的使用示例: