TermUI.Shortcut (TermUI v0.2.0)
View SourceKeyboard shortcut registry and matching.
Provides a system for registering keyboard shortcuts with actions, matching key events against registered shortcuts, and executing the associated actions.
Usage
# Create a registry
{:ok, registry} = Shortcut.start_link()
# Register shortcuts
Shortcut.register(registry, %Shortcut{
key: :q,
modifiers: [:ctrl],
action: {:message, :root, :quit},
scope: :global,
description: "Quit application"
})
# Match key event
case Shortcut.match(registry, key_event, context) do
{:ok, shortcut} -> Shortcut.execute(shortcut)
:no_match -> :ignore
end
Summary
Functions
Returns a specification to start this module under a supervisor.
Clears the partial sequence state.
Executes a shortcut's action.
Formats a shortcut for display.
Lists all registered shortcuts.
Lists shortcuts for a specific scope.
Matches a key event against registered shortcuts.
Registers a shortcut.
Starts the shortcut registry.
Unregisters a shortcut by key and modifiers.
Types
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec clear_sequence(GenServer.server()) :: :ok
Clears the partial sequence state.
Executes a shortcut's action.
Returns the result of the action execution.
Formats a shortcut for display.
Examples
iex> Shortcut.format(%Shortcut{key: :s, modifiers: [:ctrl]})
"Ctrl+S"
iex> Shortcut.format(%Shortcut{key: :q, modifiers: [:ctrl, :shift]})
"Ctrl+Shift+Q"
@spec list(GenServer.server()) :: [t()]
Lists all registered shortcuts.
@spec list_for_scope(GenServer.server(), scope()) :: [t()]
Lists shortcuts for a specific scope.
@spec match(GenServer.server(), TermUI.Event.Key.t(), map()) :: {:ok, t()} | :no_match
Matches a key event against registered shortcuts.
Returns {:ok, shortcut} if a match is found, or :no_match.
The context determines which scopes are active.
@spec register(GenServer.server(), t()) :: :ok
Registers a shortcut.
@spec start_link(keyword()) :: GenServer.on_start()
Starts the shortcut registry.
@spec unregister(GenServer.server(), atom() | String.t(), [atom()]) :: :ok
Unregisters a shortcut by key and modifiers.