Contents

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, CmdOrCtrlCommand on macOS, Control on Windows/Linux.
Command, Cmd, Meta, SuperThe Command / Meta key (macOS).
Control, CtrlThe Control key.
ShiftThe Shift key.
Alt, OptionThe Alt / Option key.
AltGrThe AltGr key (Windows/Linux).

Named key codes 

Token(s)Description
BackspaceBackspace key.
CapsLockCaps Lock key.
DeleteDelete key.
Down, Up, Left, RightArrow keys.
EndEnd key.
Enter, ReturnEnter / Return key.
Esc, EscapeEscape key.
F1F24Function keys.
HomeHome key.
InsertInsert key.
MediaNextTrackMedia Next Track key.
MediaPlayPauseMedia Play/Pause key.
MediaPreviousTrackMedia Previous Track key.
MediaStopMedia Stop key.
Num0Num9Numpad digit keys.
NumAddNumpad + key.
NumDecNumpad decimal key.
NumDivNumpad / key.
NumLockNum Lock key.
NumMultNumpad * key.
NumSubNumpad - key.
PageDownPage Down key.
PageUpPage Up key.
PlusThe + key.
PrintScreenPrint Screen key.
ScrollLockScroll Lock key.
SpaceSpace bar.
TabTab key.
VolumeDownVolume Down key.
VolumeMuteVolume Mute key.
VolumeUpVolume Up key.

Single-character key codes 

Any single ASCII character can be used as a key code directly, e.g. AZ, 09, and punctuation such as ,, -, ., /, ;, =, [, \, ], `, and '. Characters that require Shift (e.g. !, @, #, $, %, ^, &, *, (, ), +, :, <, >, ?, ", {, |, }, ~, _) implicitly add the Shift modifier.