Raxol.Core.KeyboardShortcuts (Raxol v0.2.0)

View Source

Keyboard shortcuts manager for Raxol applications.

This module provides functionality to register, manage, and handle keyboard shortcuts throughout the application. It integrates with the accessibility features to provide better keyboard navigation and interaction.

Features

  • Register global and context-specific shortcuts
  • Handle keyboard shortcuts with custom callbacks
  • Display available shortcuts based on current context
  • Integration with accessibility features
  • Support for shortcut combinations (Ctrl, Alt, Shift modifiers)

Examples

# Initialize the keyboard shortcuts manager
KeyboardShortcuts.init()

# Register a global shortcut
KeyboardShortcuts.register_shortcut("Ctrl+F", :search, fn ->
  # Search functionality
  search_content()
end)

# Register a context-specific shortcut
KeyboardShortcuts.register_shortcut("Alt+S", :save, fn ->
  # Save functionality
  save_document()
end, context: :editor)

# Get all available shortcuts for current context
shortcuts = KeyboardShortcuts.get_shortcuts_for_context(:editor)

# Display help for available shortcuts
KeyboardShortcuts.show_shortcuts_help()

Summary

Functions

Clean up the keyboard shortcuts manager.

Get the current active context for shortcuts.

Get all shortcuts for a specific context.

Initialize the keyboard shortcuts manager.

Register a keyboard shortcut with a callback function.

Set the current context for shortcuts.

Display help for available shortcuts.

Trigger a shortcut programmatically.

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.

Examples

iex> KeyboardShortcuts.cleanup()
:ok

get_current_context()

Get the current active context for shortcuts.

Examples

iex> KeyboardShortcuts.get_current_context()
:editor

get_shortcuts_for_context(context \\ nil)

Get all shortcuts for a specific context.

Parameters

  • context - The context to get shortcuts for (default: current context)

Examples

iex> KeyboardShortcuts.get_shortcuts_for_context(:editor)
[
  %{name: :save, key_combo: "Ctrl+S", description: "Save document"},
  %{name: :find, key_combo: "Ctrl+F", description: "Find in document"}
]

handle_keyboard_event(arg1)

init()

Initialize the keyboard shortcuts manager.

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

Examples

iex> KeyboardShortcuts.init()
:ok

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 (:high, :medium, :low), affects precedence

Examples

iex> KeyboardShortcuts.register_shortcut("Ctrl+S", :save, fn -> save_document() end)
:ok

iex> KeyboardShortcuts.register_shortcut("Alt+F", :file_menu, fn -> open_file_menu() end,
...>   context: :main_menu, description: "Open File menu")
:ok

set_context(context)

Set the current context for shortcuts.

This affects which shortcuts are active and will be triggered by keyboard events.

Parameters

  • context - The context to set as active

Examples

iex> KeyboardShortcuts.set_context(:editor)
:ok

iex> KeyboardShortcuts.set_context(:file_browser)
:ok

show_shortcuts_help()

Display help for available shortcuts.

This function generates a help message for all shortcuts available in the current context and announces it through the accessibility system if enabled.

Examples

iex> KeyboardShortcuts.show_shortcuts_help()
:ok

trigger_shortcut(name, context \\ nil)

Trigger a shortcut programmatically.

Parameters

  • name - The unique identifier for the shortcut
  • context - The context in which the shortcut was registered (default: current context)

Examples

iex> KeyboardShortcuts.trigger_shortcut(:save)
:ok

iex> KeyboardShortcuts.trigger_shortcut(:file_menu, :main_menu)
:ok

unregister_shortcut(name, context \\ :global)

Unregister a keyboard shortcut.

Parameters

  • name - The unique identifier for the shortcut
  • context - The context in which the shortcut was registered (default: :global)

Examples

iex> KeyboardShortcuts.unregister_shortcut(:save)
:ok

iex> KeyboardShortcuts.unregister_shortcut(:file_menu, :main_menu)
:ok