electron/docs/api/global-shortcut.md

66 lines
1.7 KiB
Markdown
Raw Normal View History

# 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.
```javascript
var app = require('app');
var globalShortcut = require('global-shortcut');
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-07-09 14:49:16 +00:00
if (!ret) {
console.log('registration failed');
}
2015-07-09 14:49:16 +00:00
// Check whether a shortcut is registered.
console.log(globalShortcut.isRegistered('ctrl+x'));
});
app.on('will-quit', function() {
2015-07-09 14:49:16 +00:00
// Unregister a shortcut.
globalShortcut.unregister('ctrl+x');
2015-07-09 14:49:16 +00:00
// Unregister all shortcuts.
globalShortcut.unregisterAll();
});
```
2015-08-27 00:32:47 +00:00
## Methods
The `global-shortcut` module has the following methods:
### `globalShortcut.register(accelerator, callback)`
* `accelerator` [Accelerator](accelerator.md)
* `callback` Function
Registers a global shortcut of `accelerator`. The `callback` is called when
the registered shortcut is pressed by the user.
2015-08-27 00:32:47 +00:00
### `globalShortcut.isRegistered(accelerator)`
* `accelerator` [Accelerator](accelerator.md)
2015-08-28 21:21:37 +00:00
Returns `true` or `false` depending on whether the shortcut `accelerator` is
registered.
2015-08-27 00:32:47 +00:00
### `globalShortcut.unregister(accelerator)`
* `accelerator` [Accelerator](accelerator.md)
2015-08-27 00:32:47 +00:00
Unregisters the global shortcut of `accelerator`.
2015-08-27 00:32:47 +00:00
### `globalShortcut.unregisterAll()`
Unregisters all the global shortcuts.