Global Shortcuts
This guide describes how to work with global keyboard shortcuts in your desktop app.
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 a global keyboard shortcut (e.g. CommandOrControl+Shift+D), 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.
To check if a global keyboard shortcut is already registered, use the following approach:
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.