2015-09-10 14:29:22 +00:00
|
|
|
# global-shortcut
|
|
|
|
|
|
|
|
`global-shortcut` 模块可以便捷的为您设置(注册/注销)各种自定义操作的快捷键.
|
|
|
|
|
|
|
|
**Note**: 使用此模块注册的快捷键是系统全局的(QQ截图那种), 不要在应用模块(app module)响应 `ready`
|
|
|
|
消息前使用此模块(注册快捷键).
|
|
|
|
|
|
|
|
```javascript
|
2016-10-03 03:47:16 +00:00
|
|
|
var app = require('app')
|
|
|
|
var globalShortcut = require('global-shortcut')
|
2015-09-10 14:29:22 +00:00
|
|
|
|
2016-10-03 03:47:16 +00:00
|
|
|
app.on('ready', function () {
|
2015-09-10 14:29:22 +00:00
|
|
|
// Register a 'ctrl+x' shortcut listener.
|
2016-10-03 03:47:16 +00:00
|
|
|
var ret = globalShortcut.register('ctrl+x', function () {
|
|
|
|
console.log('ctrl+x is pressed')
|
2015-09-10 14:29:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if (!ret) {
|
2016-10-03 03:47:16 +00:00
|
|
|
console.log('registration failed')
|
2015-09-10 14:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether a shortcut is registered.
|
2016-10-03 03:47:16 +00:00
|
|
|
console.log(globalShortcut.isRegistered('ctrl+x'))
|
|
|
|
})
|
2015-09-10 14:29:22 +00:00
|
|
|
|
2016-10-03 03:47:16 +00:00
|
|
|
app.on('will-quit', function () {
|
2015-09-10 14:29:22 +00:00
|
|
|
// Unregister a shortcut.
|
2016-10-03 03:47:16 +00:00
|
|
|
globalShortcut.unregister('ctrl+x')
|
2015-09-10 14:29:22 +00:00
|
|
|
|
|
|
|
// Unregister all shortcuts.
|
2016-10-03 03:47:16 +00:00
|
|
|
globalShortcut.unregisterAll()
|
|
|
|
})
|
2015-09-10 14:29:22 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Methods
|
|
|
|
|
|
|
|
`global-shortcut` 模块包含以下函数:
|
|
|
|
|
|
|
|
### `globalShortcut.register(accelerator, callback)`
|
|
|
|
|
|
|
|
* `accelerator` [Accelerator](accelerator.md)
|
|
|
|
* `callback` Function
|
|
|
|
|
|
|
|
注册 `accelerator` 快捷键. 当用户按下注册的快捷键时将会调用 `callback` 函数.
|
|
|
|
|
|
|
|
### `globalShortcut.isRegistered(accelerator)`
|
|
|
|
|
|
|
|
* `accelerator` [Accelerator](accelerator.md)
|
|
|
|
|
|
|
|
查询 `accelerator` 快捷键是否已经被注册过了,将会返回 `true`(已被注册) 或 `false`(未注册).
|
|
|
|
|
|
|
|
### `globalShortcut.unregister(accelerator)`
|
|
|
|
|
|
|
|
* `accelerator` [Accelerator](accelerator.md)
|
|
|
|
|
|
|
|
注销全局快捷键 `accelerator`.
|
|
|
|
|
|
|
|
### `globalShortcut.unregisterAll()`
|
|
|
|
|
|
|
|
注销本应用注册的所有全局快捷键.
|