bootstack.shortcuts.Shortcuts#

class bootstack.shortcuts.Shortcuts#

Bases: object

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
all()#

Get all registered shortcuts.

Returns:

Dictionary mapping keys to Shortcut objects.

Return type:

dict[str, Shortcut]

bind_to(window)#

Bind all registered shortcuts to a window.

Parameters:

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

Note

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

binding(key)#

Get Tkinter binding string for a shortcut key.

Parameters:

key (str) – The shortcut key to look up.

Returns:

Tkinter binding string (e.g., “<Control-s>”), or empty string if not found.

Return type:

str

display(key)#

Get display string for a shortcut key.

Parameters:

key (str) – The shortcut key to look up.

Returns:

Platform-appropriate display string (e.g., “Ctrl+S” or “⌘S”), or empty string if not found.

Return type:

str

get(key)#

Get a shortcut by key.

Parameters:

key (str) – The shortcut key to look up.

Returns:

The Shortcut object, or None if not found.

Return type:

Shortcut | None

register(key, pattern, command)#

Register a keyboard shortcut.

Parameters:
  • key (str) – Unique identifier (e.g., “save”, “file.open”)

  • 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

  • command (Callable) – Function to call when triggered

Returns:

The created Shortcut object.

Raises:

ValueError – If key is already registered.

Return type:

Shortcut

Examples

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

Remove all shortcut bindings from a window.

Parameters:

window (Any) – The window to unbind shortcuts from.

unregister(key)#

Remove a registered shortcut.

Parameters:

key (str) – The shortcut key to remove.

Raises:

KeyError – If key is not registered.