非常教程

Electron参考手册

globalShortcut

globalShortcut

当应用程序没有键盘焦点时,检测键盘事件。

程序:主

globalShortcut模块可以向操作系统注册/注销全局键盘快捷方式,以便您可以自定义各种快捷方式的操作。

注:快捷方式是全局的,即使应用程序没有键盘焦点,它也能工作。您不应该使用此模块,直到ready发出APP模块的事件。

const {app, globalShortcut} = require('electron')

app.on('ready', () => {
  // Register a 'CommandOrControl+X' shortcut listener.
  const ret = globalShortcut.register('CommandOrControl+X', () => {
    console.log('CommandOrControl+X is pressed')
  })

  if (!ret) {
    console.log('registration failed')
  }

  // Check whether a shortcut is registered.
  console.log(globalShortcut.isRegistered('CommandOrControl+X'))
})

app.on('will-quit', () => {
  // Unregister a shortcut.
  globalShortcut.unregister('CommandOrControl+X')

  // Unregister all shortcuts.
  globalShortcut.unregisterAll()
})

方法

globalShortcut模块具有以下方法:

globalShortcut.register(accelerator, callback)

  • accelerator加速器
  • callback功能

注册一个全局快捷键accelerator。在callback当注册的快捷方式被用户按下被调用。

当加速器已被其他应用程序占用时,此调用将会静默失败。这种行为是由操作系统设计的,因为它们不希望应用程序争取全局快捷方式。

globalShortcut.isRegistered(accelerator)

  • accelerator加速器返回Boolean- 此应用程序是否已注册accelerator。当加速器已被其他应用程序占用时,此调用仍将返回false。这种行为是由操作系统设计的,因为它们不希望应用程序争取全局快捷方式。globalShortcut.unregister(accelerator)
  • accelerator加速器

取消注册全球快捷方式accelerator

globalShortcut.unregisterAll()

取消注册所有全球快捷方式。

globalShortcut
globalShortcut 详细
Electron

Electron 是一个使用 JavaScript, HTML 和 CSS 等 Web 技术创建原生程序的框架,它负责比较难搞的部分,你只需把精力放在你的应用的核心上即可。

主页 https://electron.atom.io/
源码 https://github.com/electron/electron
发布版本 1.7.9