globalShortcut
当应用没有键盘焦点时检测键盘事件。
¥Detect keyboard events when the application does not have keyboard focus.
进程:主进程
¥Process: Main
globalShortcut
模块可以向操作系统注册/取消注册全局键盘快捷键,以便你可以自定义各种快捷键的操作。
¥The globalShortcut
module can register/unregister a global keyboard shortcut
with the operating system so that you can customize the operations for various
shortcuts.
注意:快捷方式是全局的;即使应用没有键盘焦点,它也可以工作。在应用模块的 ready
事件发出之前,无法使用该模块。
¥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.
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
globalShortcut
模块有以下方法:
¥The globalShortcut
module has the following methods:
globalShortcut.register(accelerator, callback)
-
accelerator
加速器¥
accelerator
Accelerator -
callback
函数¥
callback
Function
返回 boolean
- 快捷方式是否注册成功。
¥Returns boolean
- Whether or not the shortcut was registered successfully.
注册 accelerator
的全局快捷方式。当用户按下注册的快捷方式时,将调用 callback
。
¥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.
除非应用已被授权为 可信可访问性客户端,否则以下加速器将无法在 macOS 10.14 Mojave 上成功注册:
¥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
Accelerator[] - an array of Accelerators. -
callback
函数¥
callback
Function
在 accelerators
中注册所有 accelerator
项目的全局快捷方式。当用户按下任何已注册的快捷键时,将调用 callback
。
¥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.
除非应用已被授权为 可信可访问性客户端,否则以下加速器将无法在 macOS 10.14 Mojave 上成功注册:
¥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
Accelerator
返回 boolean
- 该应用是否已注册 accelerator
。
¥Returns boolean
- Whether this application has registered accelerator
.
当加速器已被其他应用占用时,此调用仍将返回 false
。这种行为是操作系统有意为之的,因为它们不希望应用争夺全局快捷方式。
¥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
取消注册 accelerator
的全局快捷方式。
¥Unregisters the global shortcut of accelerator
.
globalShortcut.unregisterAll()
取消注册所有全局快捷方式。
¥Unregisters all of the global shortcuts.