Skip to main content

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.globalShortcut模块可以在操作系统中注册/注销全局键盘快捷方式,以便您可以自定义各种快捷方式的操作。

Note: The shortcut is global; it will work even if the app does not have the keyboard focus. 快捷方式是全球性的;即使应用程序没有键盘焦点,它也能工作。This module cannot be used before the ready event of the app module is emitted.在发出应用程序模块的ready事件之前,无法使用此模块。

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

app.whenReady().then(() => {
// 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()
})

Methods方法

The globalShortcut module has the following methods:globalShortcut模块具有以下方法:

globalShortcut.register(accelerator, callback)

Returns 返回boolean - Whether or not the shortcut was registered successfully.快捷方式是否已成功注册。

Registers a global shortcut of accelerator. 注册accelerator的全局快捷方式。The callback is called when the registered shortcut is pressed by the user.当用户按下已注册的快捷方式时,会调用callback

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:除非该应用程序已被授权为受信任的无障碍客户端,否则以下加速器将无法在macOS 10.14 Mojave上成功注册:

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

globalShortcut.registerAll(accelerators, callback)

Registers a global shortcut of all accelerator items in accelerators. 注册accelerators中所有accelerator项目的全局快捷方式。The callback is called when any of the registered shortcuts are pressed by the user.当用户按下任何已注册的快捷键时,都会调用callback

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:除非该应用程序已被授权为受信任的无障碍客户端,否则以下加速器将无法在macOS 10.14 Mojave上成功注册:

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

globalShortcut.isRegistered(accelerator)

Returns 返回boolean - Whether this application has registered accelerator.此应用程序是否已注册accelerator

When the accelerator is already taken by other applications, this call will still return false. 当加速器已经被其他应用程序占用时,此调用仍将返回falseThis behavior is intended by operating systems, since they don't want applications to fight for global shortcuts.这种行为是由操作系统设计的,因为它们不希望应用程序争夺全局快捷方式。

globalShortcut.unregister(accelerator)

Unregisters the global shortcut of accelerator.注销accelerator的全局快捷方式。

globalShortcut.unregisterAll()

Unregisters all of the global shortcuts.注销所有全局快捷方式。