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)
accelerator
Acceleratorcallback
Function
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)
accelerators
string[] -an array of Accelerators.Accelerator的数组。callback
Function
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)
accelerator
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
. false
。This behavior is intended by operating systems, since they don't want applications to fight for global shortcuts.这种行为是由操作系统设计的,因为它们不希望应用程序争夺全局快捷方式。
globalShortcut.unregister(accelerator)
accelerator
Accelerator
Unregisters the global shortcut of 注销accelerator
.accelerator
的全局快捷方式。
globalShortcut.unregisterAll()
Unregisters all of the global shortcuts.注销所有全局快捷方式。