JFLib

plat/hotkey.rs#

Source: lib/plat/hotkey.rs

hotkey -- global keyboard shortcuts: chords that reach the app while another program has the
keyboard

  Chord::parse("Ctrl+Alt+K") -> Option<Chord>   modifiers ctrl, alt, shift, super (win, meta,
                                    logo), one key: a-z 0-9 f1-f24, space enter tab esc
                                    backspace delete insert home end pageup pagedown left up
                                    right down print pause num0-num9 numadd numsub nummul
                                    numdiv numdec minus plus comma period (case ignored)
  chord.text() -> "Ctrl+Alt+K"      chord.trigger() -> "CTRL+ALT+k" (the XDG shortcuts
                                    notation of the GlobalShortcuts portal)
  hotkey_available() -> bool        global shortcuts work here
  hotkey_register(id, chord, description) -> Result<(), HotkeyError>   id != 0; the same id
                                    again replaces its chord; needs `input.global` in the
                                    manifest (else Denied, "PERMDENY input.global")
  hotkey_unregister(id) -> bool     hotkey_clear()   hotkey_list() -> [(id, chord)]
  hotkey_take() -> Vec<HotkeyEvent> Pressed(id) when the chord was pressed anywhere (held keys
                                    do not repeat), Refused(id, why) when the desktop refused
                                    a registration later (the portal); each one calls wake()
  set_dry_run(on)                   only record registrations (the UI's headless runs);
                                    hotkey_test_press(id) delivers a press (test aid)

Call register / unregister from the UI thread: the presses arrive through its event loop.
Windows (win_hotkey.rs): RegisterHotKey (MOD_NOREPEAT) on a message-only window of the UI
thread, WM_HOTKEY; a chord another program holds is Conflict.  X11 (linux_x11_screen.rs):
XGrabKey on the root window for every keycode of the key and every combination of Caps Lock
and Num Lock, KeyPress on the root window; BadAccess (another client grabbed it) is
Conflict.  Wayland and Flatpak (linux_hotkey.rs): the GlobalShortcuts portal on a thread of
its own: a session, BindShortcuts with the description and the preferred trigger -- the
desktop asks the user, who may choose another chord or none; what is not bound comes as
Refused(id, Dismissed); Activated -> Pressed.  macOS (apple_mac_services.rs): Carbon's
RegisterEventHotKey on the application's event target (no permission needed; Command acts as
Ctrl, Control as Super); a chord another program holds is Conflict.  Browser and Android:
Unavailable.
  • struct Chord a key with modifiers: VK code (the platform's E_KEY_DOWN codes) and MOD_SHIFT | MOD_CTRL | MOD_ALT | MOD_SUPER

    • vk: u32
    • mods: u8
  • enum HotkeyError why a shortcut was not registered

    • Unavailable -- no global shortcuts here (browser, Android, no X server / portal)
    • Denied -- the app's permissions lack input.global
    • Invalid -- id 0, a key without a name, a description with NUL
    • Conflict -- another program (or another id of this app) holds the chord
    • Dismissed -- the user or the desktop did not bind it (the portal's dialog)
    • Failed(String) -- the system failed: what it said
  • enum HotkeyEvent what happened to a registered shortcut

    • Pressed(u32) -- its chord was pressed
    • Refused(u32, HotkeyError) -- the desktop did not bind it after all (the portal answers later)

impl Chord

  • fn parse(s: &str) -> Option<Chord> "Ctrl+Alt+K", "super+f12", "Shift+NumAdd" -> the chord (None: no key, an unknown name, two keys)

  • fn text(&self) -> String "Ctrl+Alt+K" (the modifiers in the order Ctrl, Alt, Shift, Super)

  • fn trigger(&self) -> Option<String> the XDG shortcuts notation ("CTRL+ALT+k", "LOGO+F12"): modifiers CTRL ALT SHIFT LOGO, the key as its xkb keysym name; None for a key without one

  • fn hotkey_take() -> Vec<HotkeyEvent> what happened since the last call, oldest first

  • fn set_dry_run(on: bool) only record registrations from now on (the UI's headless runs: scripts, --shot)

  • fn hotkey_available() -> bool whether global shortcuts work here

  • fn hotkey_register(id: u32, chord: Chord, description: &str) -> Result<(), HotkeyError> register chord as the global shortcut id (the same id again replaces it); description says what it does (the portal's dialog shows it)

  • fn hotkey_unregister(id: u32) -> bool remove the shortcut id -> false when it was not registered

  • fn hotkey_clear() remove every shortcut of the app

  • fn hotkey_list() -> Vec<(u32, Chord)> the registered shortcuts: (id, chord)

  • fn hotkey_test_press(id: u32) -> bool (test aid) a press of the registered shortcut id, as the system would report it -> false when id is not registered

Source: docs/API.md in the JFLib repository.