Skip to content

Shortcuts

Cross-platform keyboard shortcut management.

get_shortcuts

Get the global Shortcuts service instance.

Returns:

Type Description
Shortcuts

The singleton Shortcuts instance.

Examples:

import bootstack as bs

shortcuts = bs.get_shortcuts()
shortcuts.register("save", "Mod+S", save_file)
shortcuts.bind_to(app)

Shortcuts

Singleton service for managing keyboard shortcuts.

Provides cross-platform keyboard shortcut registration and binding. The service automatically translates modifier keys for each platform:

  • Mod becomes Ctrl on Windows/Linux, Command on Mac
  • Alt becomes Alt on Windows/Linux, Option on Mac
  • Shift works the same on all platforms

Examples:

shortcuts = get_shortcuts()

# Register shortcuts
shortcuts.register("save", "Mod+S", save_file)
shortcuts.register("undo", "Mod+Z", undo)
shortcuts.register("find", "Mod+F", find)

# Bind all to window
shortcuts.bind_to(app)

# Get display string for menu
shortcuts.display("save")  # "Ctrl+S" on Windows, "⌘S" on Mac

register

register(
    key: str, pattern: str, command: Callable
) -> Shortcut

Register a keyboard shortcut.

Parameters:

Name Type Description Default
key str

Unique identifier (e.g., "save", "file.open")

required
pattern str

Shortcut pattern using symbolic modifiers: - Mod+S - Primary modifier + S - Mod+Shift+S - Primary modifier + Shift + S - Alt+F4 - Alt + F4 - F5 - Function key alone

required
command Callable

Function to call when triggered

required

Returns:

Type Description
Shortcut

The created Shortcut object.

Raises:

Type Description
ValueError

If key is already registered.

Examples:

shortcuts.register("save", "Mod+S", save_file)
shortcuts.register("quit", "Mod+Q", app.quit)
shortcuts.register("refresh", "F5", refresh)

unregister

unregister(key: str) -> None

Remove a registered shortcut.

Parameters:

Name Type Description Default
key str

The shortcut key to remove.

required

Raises:

Type Description
KeyError

If key is not registered.

get

get(key: str) -> Shortcut | None

Get a shortcut by key.

Parameters:

Name Type Description Default
key str

The shortcut key to look up.

required

Returns:

Type Description
Shortcut | None

The Shortcut object, or None if not found.

display

display(key: str) -> str

Get display string for a shortcut key.

Parameters:

Name Type Description Default
key str

The shortcut key to look up.

required

Returns:

Type Description
str

Platform-appropriate display string (e.g., "Ctrl+S" or "⌘S"),

str

or empty string if not found.

binding

binding(key: str) -> str

Get Tkinter binding string for a shortcut key.

Parameters:

Name Type Description Default
key str

The shortcut key to look up.

required

Returns:

Type Description
str

Tkinter binding string (e.g., ""),

str

or empty string if not found.

bind_to

bind_to(window: Any) -> None

Bind all registered shortcuts to a window.

Parameters:

Name Type Description Default
window Any

The window (App, Toplevel, Tk) to bind shortcuts to.

required
Note

Shortcuts registered after calling bind_to will also be automatically bound to this window.

unbind_from

unbind_from(window: Any) -> None

Remove all shortcut bindings from a window.

Parameters:

Name Type Description Default
window Any

The window to unbind shortcuts from.

required

all

all() -> dict[str, Shortcut]

Get all registered shortcuts.

Returns:

Type Description
dict[str, Shortcut]

Dictionary mapping keys to Shortcut objects.

Shortcut

A registered keyboard shortcut.

Attributes:

Name Type Description
key str

Unique identifier for lookup (e.g., "save", "file.open")

pattern str

Shortcut pattern (e.g., "Mod+S", "Shift+Alt+N")

command Callable

Function to execute when triggered

display property

display: str

Platform-appropriate display string for menus.

Returns:

Type Description
str

On Mac: "⇧⌘S" (symbols, no separators)

str

On Windows/Linux: "Ctrl+Shift+S" (names with + separators)

binding property

binding: str

Tkinter binding string.

Returns:

Type Description
str

Binding string like "" or ""