To allow a widget to capture user input, you must define a controls attribute. This attribute is a dictionary that maps specific keys to handler functions. When a key is pressed while the widget is active, the corresponding function is executed.
Key Types
The dictionary keys can be one of two types:
Char: A single character (e.g., 'q', 'h', or ']').KeyInput: Special keys such as ArrowLeft(), HomeKey(), EndKey(), Esc(), or SpaceBar().
Function Signature
Each handler function must follow the signature fn(w, k), where:
w: The widget instance the function is assigned to.k: The key that was pressed.
Because a single function might handle multiple different keys (e.g., both ArrowRight() and ']' triggering a page down), the function signature should use a Union type to accommodate the possible key inputs.
# Example: Defining a handler that accepts multiple key types
next_page(p::Pager, ::Union{PageDownKey, ArrowRight, Char})
# Example: Mapping keys to handlers in the controls dictionary
pager_controls = Dict(
ArrowRight() => next_page,
']' => next_page,
ArrowLeft() => prev_page,
'[' => prev_page,
ArrowDown() => next_line,
'.' => next_line,
ArrowUp() => prev_line,
',' => prev_line,
HomeKey() => home,
EndKey() => toend,
Esc() => quit,
'q' => quit,
)