2014-08-04 01:47:42 +00:00
|
|
|
# global-shortcut
|
|
|
|
|
|
|
|
The `global-shortcut` module can register/unregister a global keyboard shortcut
|
2015-08-27 00:32:47 +00:00
|
|
|
with the operating system so that you can customize the operations for various
|
|
|
|
shortcuts.
|
|
|
|
|
2015-08-28 21:21:37 +00:00
|
|
|
**Note**: The shortcut is global; it will work even if the app does
|
2015-08-27 00:32:47 +00:00
|
|
|
not have the keyboard focus. You should not use this module until the `ready`
|
|
|
|
event of the app module is emitted.
|
2014-08-04 01:47:42 +00:00
|
|
|
|
|
|
|
```javascript
|
2015-07-09 09:03:58 +00:00
|
|
|
var app = require('app');
|
2014-08-04 01:47:42 +00:00
|
|
|
var globalShortcut = require('global-shortcut');
|
|
|
|
|
2015-07-09 09:03:58 +00:00
|
|
|
app.on('ready', function() {
|
2015-07-09 14:49:16 +00:00
|
|
|
// Register a 'ctrl+x' shortcut listener.
|
|
|
|
var ret = globalShortcut.register('ctrl+x', function() {
|
|
|
|
console.log('ctrl+x is pressed');
|
|
|
|
})
|
2015-06-09 16:01:09 +00:00
|
|
|
|
2015-07-09 14:49:16 +00:00
|
|
|
if (!ret) {
|
|
|
|
console.log('registration failed');
|
|
|
|
}
|
2014-08-04 01:47:42 +00:00
|
|
|
|
2015-07-09 14:49:16 +00:00
|
|
|
// Check whether a shortcut is registered.
|
|
|
|
console.log(globalShortcut.isRegistered('ctrl+x'));
|
2015-07-09 09:03:58 +00:00
|
|
|
});
|
2014-08-04 01:47:42 +00:00
|
|
|
|
2015-07-09 09:03:58 +00:00
|
|
|
app.on('will-quit', function() {
|
2015-07-09 14:49:16 +00:00
|
|
|
// Unregister a shortcut.
|
|
|
|
globalShortcut.unregister('ctrl+x');
|
2014-08-04 01:47:42 +00:00
|
|
|
|
2015-07-09 14:49:16 +00:00
|
|
|
// Unregister all shortcuts.
|
|
|
|
globalShortcut.unregisterAll();
|
2015-07-09 09:03:58 +00:00
|
|
|
});
|
2014-08-04 01:47:42 +00:00
|
|
|
```
|
|
|
|
|
2015-08-27 00:32:47 +00:00
|
|
|
## Methods
|
|
|
|
|
|
|
|
The `global-shortcut` module has the following methods:
|
|
|
|
|
|
|
|
### `globalShortcut.register(accelerator, callback)`
|
2014-08-04 01:47:42 +00:00
|
|
|
|
2014-08-04 16:00:39 +00:00
|
|
|
* `accelerator` [Accelerator](accelerator.md)
|
2014-08-04 01:47:42 +00:00
|
|
|
* `callback` Function
|
|
|
|
|
2015-07-14 15:58:15 +00:00
|
|
|
Registers a global shortcut of `accelerator`. The `callback` is called when
|
|
|
|
the registered shortcut is pressed by the user.
|
2014-08-04 01:47:42 +00:00
|
|
|
|
2015-08-27 00:32:47 +00:00
|
|
|
### `globalShortcut.isRegistered(accelerator)`
|
2014-08-04 01:47:42 +00:00
|
|
|
|
2014-08-04 16:00:39 +00:00
|
|
|
* `accelerator` [Accelerator](accelerator.md)
|
2014-08-04 01:47:42 +00:00
|
|
|
|
2015-08-28 21:21:37 +00:00
|
|
|
Returns `true` or `false` depending on whether the shortcut `accelerator` is
|
|
|
|
registered.
|
2014-08-04 01:47:42 +00:00
|
|
|
|
2015-08-27 00:32:47 +00:00
|
|
|
### `globalShortcut.unregister(accelerator)`
|
2014-08-04 01:47:42 +00:00
|
|
|
|
2014-08-04 16:00:39 +00:00
|
|
|
* `accelerator` [Accelerator](accelerator.md)
|
2014-08-04 01:47:42 +00:00
|
|
|
|
2015-08-27 00:32:47 +00:00
|
|
|
Unregisters the global shortcut of `accelerator`.
|
2014-08-04 01:47:42 +00:00
|
|
|
|
2015-08-27 00:32:47 +00:00
|
|
|
### `globalShortcut.unregisterAll()`
|
2014-08-04 01:47:42 +00:00
|
|
|
|
|
|
|
Unregisters all the global shortcuts.
|