Contents

Global Shortcut

This guide describes how to configure global keyboard shortcuts for your application.

The global keyboard shortcuts are triggered even if the application does not have the keyboard focus. You can use them to implement a global hotkey for your application.

To register a global keyboard shortcut (e.g. CommandOrControl+Shift+V), use the following approach:

bool success = app->globalShortcuts()->registerShortcut(
    Shortcut(KeyCode::V, KeyModifier::COMMAND_OR_CTRL | KeyModifier::SHIFT),
    [](const Shortcut& shortcut) {
      std::cout << "Shortcut pressed!" << std::endl;
    });

The registerShortcut() method returns true if the shortcut has been registered successfully. The shortcut registration may fail if the same global shortcut is already registered by any other running application or if it is a system-wide shortcut that cannot be overridden.

Note: In the code above, the COMMAND_OR_CTRL flag indicates that on macOS the ⌘ Сmd key should be used, and Ctrl on Windows/Linux.

To unregister a global keyboard shortcut, use the following approach:

app->globalShortcuts()->unregisterShortcut(
    Shortcut(KeyCode::V, KeyModifier::COMMAND_OR_CTRL | KeyModifier::SHIFT));

When the application is closed, all registered global shortcuts are automatically unregistered.

On this page
Top