beetlebug

ztk::hotkey

Registers a global shortcut: a key sequence that runs a script also when another app has the focus.

Synopsis

ztk::hotkey add SEQUENCE SCRIPT
ztk::hotkey remove NAME
ztk::hotkey names

Arguments

Name Type Default Meaning
add SEQUENCE SCRIPT subcommand Registers the shortcut and gives its name. SCRIPT runs at the global level when the keys go down.
remove NAME subcommand Removes the shortcut.
names subcommand Gives the names of the shortcuts, oldest first.
SEQUENCE key sequence Modifiers and one key symbol, joined with -, with or without the angle brackets of bind: <Control-Alt-k> or Control-Alt-k. The modifiers are Control, Alt, Shift, and Super, also Command and Win for Super. An upper case letter adds Shift.

A shortcut needs a grant: Options.hotkeys of the embedder, or ztk --allow hotkey.

Return value

add gives the name of the shortcut: hotkey1, hotkey2, and so on. names gives a list of names. remove gives an empty string.

Errors

Error When
global shortcuts need a grant of the embedder (ztk --allow hotkey) add with no grant.
this platform has no global shortcuts add on a backend with no global shortcuts.
bad shortcut "X" An unknown modifier or key symbol.
the system refused the shortcut "X": another app may have it The system did not register the keys. On Wayland, also a desktop with no portal of global shortcuts, or an app with no desktop file.
shortcut "NAME" doesn't exist remove of an unknown name.
bad option "X": must be add, names, or remove An unknown subcommand.

Platforms

Platform Works
Windows Yes: RegisterHotKey.
macOS No
Linux, Wayland Yes: the portal org.freedesktop.portal.GlobalShortcuts. The desktop decides the keys.
Linux, X11 No
Web No
iOS, Android No
Headless Yes: the backend keeps the shortcut.

Example

# Run with: ztk --allow hotkey FILE
if {![catch {ztk::hotkey add <Control-Alt-k> {wm deiconify .; raise .}} name]} {
    puts [ztk::hotkey names]
    ztk::hotkey remove $name
}

See also