Raxol.Core.KeyboardShortcuts (Raxol v2.0.1)

View Source

Refactored KeyboardShortcuts that delegates to GenServer implementation.

This module provides the same API as the original KeyboardShortcuts but uses a supervised GenServer instead of the Process dictionary for state management.

Migration Notice

This module is a drop-in replacement for Raxol.Core.KeyboardShortcuts. All functions maintain backward compatibility while providing improved fault tolerance and functional programming patterns.

Benefits over Process Dictionary

  • Supervised state management with fault tolerance
  • Pure functional shortcut resolution
  • Priority-based conflict resolution
  • Context-aware shortcut activation
  • Better debugging and testing capabilities
  • No global state pollution

Summary

Functions

Clean up the keyboard shortcuts manager.

Clear all shortcuts.

Clear shortcuts for a specific context.

Check if shortcuts are enabled.

Ensures the Keyboard Shortcuts server is started.

Get the currently active context.

Get all available shortcuts (global + active context).

Get all shortcuts for a specific context.

Get formatted help text for shortcuts.

Initialize the keyboard shortcuts manager.

Register a batch of shortcuts at once.

Register a keyboard shortcut with a callback function.

Set the active context for shortcuts.

Set conflict resolution strategy.

Enable or disable shortcut processing.

Check if a shortcut is registered.

Show help text for available shortcuts.

Unregister a keyboard shortcut.

Functions

cleanup()

Clean up the keyboard shortcuts manager.

This function cleans up any resources used by the keyboard shortcuts manager and unregisters event handlers.

clear_all()

Clear all shortcuts.

clear_context(context)

Clear shortcuts for a specific context.

enabled?()

Check if shortcuts are enabled.

ensure_started()

Ensures the Keyboard Shortcuts server is started.

get_active_context()

Get the currently active context.

get_available_shortcuts()

Get all available shortcuts (global + active context).

get_shortcuts_for_context(context \\ nil)

Get all shortcuts for a specific context.

Returns a map of shortcut definitions for the given context.

get_shortcuts_help()

Get formatted help text for shortcuts.

handle_keyboard_event(event_type, event_data)

Handle keyboard events.

This function is called by the EventManager when keyboard events occur.

init()

Initialize the keyboard shortcuts manager.

This function sets up the necessary state for managing keyboard shortcuts and registers event handlers for keyboard events.

register_batch(shortcuts)

Register a batch of shortcuts at once.

Example

register_batch([
  {"Ctrl+S", :save, &save_file/0, description: "Save file"},
  {"Ctrl+O", :open, &open_file/0, description: "Open file"},
  {"Ctrl+Q", :quit, &quit_app/0, description: "Quit application"}
])

register_shortcut(shortcut, name, callback, opts \\ [])

Register a keyboard shortcut with a callback function.

Parameters

  • shortcut - The keyboard shortcut string (e.g., "Ctrl+S", "Alt+F4")
  • name - A unique identifier for the shortcut (atom or string)
  • callback - A function to be called when the shortcut is triggered
  • opts - Options for the shortcut

Options

  • :context - The context in which this shortcut is active (default: :global)
  • :description - A description of what the shortcut does
  • :priority - Priority level (1-10, lower = higher priority)
  • :override - Whether to override existing shortcut (default: false)

set_active_context(context)

Set the active context for shortcuts.

Context-specific shortcuts will only be active when their context is set.

set_conflict_resolution(strategy)

Set conflict resolution strategy.

Strategies

  • :first - Keep the first registered shortcut
  • :last - Keep the last registered shortcut
  • :priority - Use priority to resolve conflicts

set_enabled(enabled)

Enable or disable shortcut processing.

shortcut_registered?(shortcut, context \\ :global)

Check if a shortcut is registered.

show_shortcuts_help()

Show help text for available shortcuts.

Displays formatted help text for all available shortcuts in the current context.

unregister_shortcut(shortcut, context \\ :global)

Unregister a keyboard shortcut.

Parameters

  • shortcut - The keyboard shortcut string to unregister
  • context - The context from which to unregister (default: :global)