Skip to main content

Module keys

Module keys 

Source
Expand description

Global shortcuts: the key bindings of the system scope, registered with the desktop so their command runs while the application is not focused.

Experimental: may change in any release (docs/stability.md).

A command declares one like any key, with a scope: CommandSpec::key_with("Ctrl+Alt+P", BindOptions::system()) (system in keymap files). It shows in the shortcuts editor, the user rebinds it there (it stays in the system scope), and inside the window it fires as a global binding does. SystemKeysPlugin registers the live ones (AppHandle::system_bindings) with the desktop at start and after each change, and says how it went through the SYSTEM_KEYS service (egui_pop_api::system_keys), which the shortcuts editor shows: also outside the window, waiting for the desktop, refused and why, or only in the window because the desktop takes no global shortcuts.

Its step (ADR 0011):

  • keys.system: a global shortcut was pressed, { "command", "keys", "backend" }. By default the command runs through the host. Veto it while a modal task runs; replace it to bring the window up first.

Backends: HotKeys through global-hotkey (Windows, macOS, X11), Portal through the XDG GlobalShortcuts portal on Wayland (GNOME 48 and later, KDE Plasma 5.27 and later; the desktop asks the user to accept the shortcuts, and may let them pick other keys), detect choosing between them, and Simulated for tests.

A global shortcut is one chord (Ctrl+Alt+P); a sequence (Ctrl+K Ctrl+P) is refused, and the editor says so. Ctrl is Command on macOS, as in egui-pop’s other bindings.

use egui_pop_api::App;
use egui_pop_api::system_keys::{KeyStatus, SYSTEM_KEYS};
use egui_pop_api::{Ctx, Plugin, PluginError};
use egui_pop_model::commands::CommandSpec;
use egui_pop_model::ids::{CommandId, PluginId};
use egui_pop_model::keymap::BindOptions;
use egui_pop_platform::keys::{Simulated, SystemKeysPlugin};

struct Player;

impl Plugin for Player {
    fn id(&self) -> PluginId { PluginId::from_static("player") }
    fn connect(&mut self, ctx: &mut Ctx<'_>) -> Result<(), PluginError> {
        ctx.command(
            CommandSpec::new(CommandId::from_static("player.toggle"), "Play or pause", "Player")
                .key_with("Ctrl+Alt+P", BindOptions::system()),
            |_| Ok(serde_json::Value::Null),
        )?;
        Ok(())
    }
}

let desktop = Simulated::default();
let ctx = egui::Context::default();
let mut app = App::builder()
    .plugin(Player)
    .plugin(SystemKeysPlugin::new("org.example.Player").backend(desktop.clone()))
    .build(&ctx)
    .unwrap();
app.logic(&ctx);
let keys = app.handle().service(&SYSTEM_KEYS).unwrap();
assert_eq!(keys.status("Ctrl+Alt+P"), Some(KeyStatus::Registered));
desktop.press("Ctrl+Alt+P");
app.logic(&ctx);
assert_eq!(app.handle().history()[0].id.as_str(), "player.toggle");

Structs§

HotKeys
Global shortcuts through global-hotkey: Windows, macOS, X11.
Portal
Global shortcuts through the XDG GlobalShortcuts portal (Wayland). Its thread holds a portal session; each registration replaces the session, and the desktop asks the user to accept the shortcuts.
Presses
Hands presses to the plugin from any thread, waking the interface.
Shortcut
A shortcut to register.
Simulated
A desktop that records what is registered and presses keys as a user would. For tests and the gallery.
SystemKeysPlugin
Global shortcuts, as a plugin (see the module).

Constants§

PLUGIN
The plugin’s id.
STEP_PRESSED
A global shortcut was pressed: { "command", "keys", "backend" }.

Traits§

Backend
What registers global shortcuts with the desktop. It lives on the interface thread (global-hotkey’s manager is tied to it on Windows and macOS); its threads write each registration’s state to status.

Functions§

code_of
global-hotkey’s code for an egui key.
detect
The backend for this session: the portal on Wayland, global-hotkey elsewhere (nothing on the web).
hotkey_of
The hotkey of a chord, or why there is none.
xdg_trigger
The XDG shortcuts specification’s trigger for a chord (CTRL+ALT+p), as the GlobalShortcuts portal takes it.