当前位置: 首页 > 文档资料 > Electron 中文文档 >

系统快捷键

优质
小牛编辑
158浏览
2023-12-01

在应用程序没有键盘焦点时,监听键盘事件。

线程:主线程

globalShortcut 模块可以在操作系统中注册/注销全局快捷键, 以便可以为操作定制各种快捷键。

注意: 快捷方式是全局的; 即使应用程序没有键盘焦点, 它也仍然在持续监听键盘事件。 在应用程序模块发出 ready 事件之前, 不应使用此模块。

const { app, globalShortcut } = require('electron')
app.on('ready', () => {
  // 注册一个 'CommandOrControl+X' 的全局快捷键
  const ret = globalShortcut.register('CommandOrControl+X', () => {
    console.log('CommandOrControl+X is pressed')
  })
  if (!ret) {
    console.log('registration failed')
  }
  // 检查快捷键是否注册成功
  console.log(globalShortcut.isRegistered('CommandOrControl+X'))
})
app.on('will-quit', () => {
  // 注销快捷键
  globalShortcut.unregister('CommandOrControl+X')
  // 注销所有快捷键
  globalShortcut.unregisterAll()
})

globalShortcut

Detect keyboard events when the application does not have keyboard focus.

Process: Main

The globalShortcut module can register/unregister a global keyboard shortcut with the operating system so that you can customize the operations for various shortcuts.

Note: The shortcut is global; it will work even if the app does not have the keyboard focus. You should not use this module until the ready event of the app module is emitted.

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 模块具有以下方法:

Methods

The globalShortcut module has the following methods:

globalShortcut.register(accelerator, callback)

  • accelerator Accelerator
  • callback Function

Returns Boolean - Whether or not the shortcut was registered successfully.

注册指定的 accelerator 为全局快捷键。当用户按下该注册的快捷键时, 将调用 callback回调函数。

如果指定的快捷键已经被其他应用程序注册掉, 调用会默默失败。 该特性由操作系统定义,因为操作系统不希望多个程序的全局快捷键互相冲突。

在 macOS 10.14 Mojave 下面,如果 app 没有被授权为可信任使用的客户端,那么下列快捷键会注册失败:

  • "Media Play/Pause"
  • "Media Next Track"
  • "Media Previous Track"
  • "Media Stop"

globalShortcut.register(accelerator, callback)

  • accelerator Accelerator
  • callback Function

Returns Boolean - Whether or not the shortcut was registered successfully.

Registers a global shortcut of accelerator. The callback is called when the registered shortcut is pressed by the user.

When the accelerator is already taken by other applications, this call will silently fail. This behavior is intended by operating systems, since they don't want applications to fight for global shortcuts.

The following accelerators will not be registered successfully on macOS 10.14 Mojave unless the app has been authorized as a trusted accessibility client:

  • "Media Play/Pause"
  • "Media Next Track"
  • "Media Previous Track"
  • "Media Stop"

globalShortcut.registerAll(accelerators, callback)

  • accelerators String[] - an array of Accelerators.
  • callback Function

Registers a global shortcut of all accelerator items in accelerators. The callback is called when any of the registered shortcuts are pressed by the user.

When a given accelerator is already taken by other applications, this call will silently fail. 该特性由操作系统定义,因为操作系统不希望多个程序的全局快捷键互相冲突。

在 macOS 10.14 Mojave 下面,如果 app 没有被授权为可信任使用的客户端,那么下列快捷键会注册失败:

  • "Media Play/Pause"
  • "Media Next Track"
  • "Media Previous Track"
  • "Media Stop"

globalShortcut.registerAll(accelerators, callback)

  • accelerators String[] - an array of Accelerators.
  • callback Function

Registers a global shortcut of all accelerator items in accelerators. The callback is called when any of the registered shortcuts are pressed by the user.

When a given accelerator is already taken by other applications, this call will silently fail. This behavior is intended by operating systems, since they don't want applications to fight for global shortcuts.

The following accelerators will not be registered successfully on macOS 10.14 Mojave unless the app has been authorized as a trusted accessibility client:

  • "Media Play/Pause"
  • "Media Next Track"
  • "Media Previous Track"
  • "Media Stop"

globalShortcut.isRegistered(accelerator)

  • accelerator Accelerator

Returns Boolean - 表示 accelerator 全局快捷键是否注册成功

当快捷键已经被其他应用程序注册时, 此调用将返回 false。 该特性由操作系统定义,因为操作系统不希望多个程序的全局快捷键互相冲突。

globalShortcut.isRegistered(accelerator)

  • accelerator Accelerator

Returns Boolean - Whether this application has registered accelerator.

When the accelerator is already taken by other applications, this call will still return false. This behavior is intended by operating systems, since they don't want applications to fight for global shortcuts.

globalShortcut.unregister(accelerator)

  • accelerator Accelerator

注销 accelerator 的全局快捷键。

globalShortcut.unregister(accelerator)

  • accelerator Accelerator

Unregisters the global shortcut of accelerator.

globalShortcut.unregisterAll()

注销所有的全局快捷键(清空该应用程序的全局快捷键)。

globalShortcut.unregisterAll()

Unregisters all of the global shortcuts.