Global Shortcuts
This guide describes how to work with global keyboard shortcuts in your desktop app.
Overview
The global keyboard shortcuts are triggered even if the application does not have the keyboard focus. You can use them to implement global hotkeys for your application.
To register the CommandOrControl+Shift+D global keyboard shortcut, you can use the following approach:
import { globalShortcut } from '@mobrowser/api';
const success = globalShortcut.register('CommandOrControl+Shift+D', () => {
console.log('CommandOrControl+Shift+D has been pressed')
})
The function returns true if the shortcut has been registered successfully.
The shortcut registration may fail if the same global shortcut is already registered by this method or the given shortcut cannot be registered due to the operating system restrictions.
Note: In the code above, the CommandOrControl part indicates that on macOS the ⌘ (Command) key should be used, and Ctrl on Windows/Linux.
You can also check if a global keyboard shortcut is already registered:
if (globalShortcut.isRegistered('CommandOrControl+Shift+D')) {
console.log('CommandOrControl+Shift+D is already registered')
}
To unregister a global keyboard shortcut, use the following approach:
globalShortcut.unregister('CommandOrControl+Shift+D')
When the application is closed, all registered global shortcuts are automatically unregistered.
Shortcut format
The shortcut is a string in the format 'CommandOrControl+Shift+F'.
Tokens are separated by + and are case-insensitive. The last token must be a key code. All preceding tokens must be modifiers.
Modifiers
| Token(s) | Description |
|---|---|
CommandOrControl, CmdOrCtrl | Command on macOS, Control on Windows/Linux. |
Command, Cmd, Meta, Super | The Command / Meta key (macOS). |
Control, Ctrl | The Control key. |
Shift | The Shift key. |
Alt, Option | The Alt / Option key. |
AltGr | The AltGr key (Windows/Linux). |
Named key codes
| Token(s) | Description |
|---|---|
Backspace | Backspace key. |
CapsLock | Caps Lock key. |
Delete | Delete key. |
Down, Up, Left, Right | Arrow keys. |
End | End key. |
Enter, Return | Enter / Return key. |
Esc, Escape | Escape key. |
F1–F24 | Function keys. |
Home | Home key. |
Insert | Insert key. |
MediaNextTrack | Media Next Track key. |
MediaPlayPause | Media Play/Pause key. |
MediaPreviousTrack | Media Previous Track key. |
MediaStop | Media Stop key. |
Num0–Num9 | Numpad digit keys. |
NumAdd | Numpad + key. |
NumDec | Numpad decimal key. |
NumDiv | Numpad / key. |
NumLock | Num Lock key. |
NumMult | Numpad * key. |
NumSub | Numpad - key. |
PageDown | Page Down key. |
PageUp | Page Up key. |
Plus | The + key. |
PrintScreen | Print Screen key. |
ScrollLock | Scroll Lock key. |
Space | Space bar. |
Tab | Tab key. |
VolumeDown | Volume Down key. |
VolumeMute | Volume Mute key. |
VolumeUp | Volume Up key. |
Single-character key codes
Any single ASCII character can be used as a key code directly, e.g. A–Z,
0–9, and punctuation such as ,, -, ., /, ;, =, [, \,
], `, and '. Characters that require Shift (e.g. !, @, #,
$, %, ^, &, *, (, ), +, :, <, >, ?, ", {,
|, }, ~, _) implicitly add the Shift modifier.