ConsoleForge 0.3.0
Elm-architecture TUI framework for .NET 8
Loading...
Searching...
No Matches
ConsoleForge.Core.KeyMap Class Referencesealed

Declarative input binding map. More...

Public Member Functions

IMsgHandle (IMsg msg)
 Try to resolve an input message to an application message.
IMsgHandleKey (KeyMsg key)
 Try to resolve a keyboard message. Returns null if no binding matches.
IMsgHandleMouse (MouseMsg mouse)
 Try to resolve a mouse message. Returns null if no binding matches.
KeyMap Merge (KeyMap other)
 Create a new KeyMap containing all bindings from this map followed by all bindings from other .
KeyMap On (ConsoleKey key, Func< IMsg > handler)
 Bind a key (any modifiers) to a message factory.
KeyMap On (ConsoleKey key, Func< KeyMsg, IMsg > handler)
 Bind a key (any modifiers) to a message factory that receives the original KeyMsg.
KeyMap On (KeyPattern pattern, Func< IMsg > handler)
 Bind a KeyPattern to a message factory.
KeyMap On (KeyPattern pattern, Func< KeyMsg, IMsg > handler)
 Bind a KeyPattern to a message factory that receives the original KeyMsg.
KeyMap OnClick (Func< MouseMsg, IMsg > handler)
 Bind left-click press events.
KeyMap OnMouse (Func< MouseMsg, bool > predicate, Func< MouseMsg, IMsg > handler)
 Bind any mouse event matching predicate .
KeyMap OnMouse (MouseButton button, MouseAction action, Func< MouseMsg, IMsg > handler)
 Bind a specific mouse button + action combination.
KeyMap OnScroll (Func< MouseMsg, IMsg > handler)
 Bind scroll-wheel events (both up and down).

Properties

int KeyBindingCount [get]
 Number of key bindings registered.
int MouseBindingCount [get]
 Number of mouse bindings registered.

Detailed Description

Declarative input binding map.

Resolves KeyMsg and MouseMsg to application IMsg values without switch statements.

Usage:

static readonly KeyMap Nav = new KeyMap()
.On(ConsoleKey.UpArrow, () => new NavUpMsg())
.On(ConsoleKey.DownArrow, () => new NavDownMsg())
.On(ConsoleKey.Escape, () => new QuitMsg())
.OnScroll(m => m.Button == MouseButton.ScrollUp
? new NavUpMsg() : new NavDownMsg());
// In Update:
if (Nav.Handle(msg) is { } result)
return ProcessAction(result);
Declarative input binding map.
Definition KeyMap.cs:28
KeyMap On(ConsoleKey key, Func< IMsg > handler)
Bind a key (any modifiers) to a message factory.
Definition KeyMap.cs:35
KeyMap OnScroll(Func< MouseMsg, IMsg > handler)
Bind scroll-wheel events (both up and down).
Definition KeyMap.cs:72
record QuitMsg
Signals the program loop to exit cleanly.
Definition Messages.cs:6
MouseButton
Dispatched by List when the user presses Enter on a selected item.
Definition Messages.cs:62

Bindings are evaluated in registration order; the first match wins.

The map is mutable during construction (fluent builder) but should be stored as a static readonly field once built. It is thread-safe for reads.

Member Function Documentation

◆ Handle()

IMsg? ConsoleForge.Core.KeyMap.Handle ( IMsg msg)

Try to resolve an input message to an application message.

Returns null if no binding matches. Handles both KeyMsg and MouseMsg.

◆ Merge()

KeyMap ConsoleForge.Core.KeyMap.Merge ( KeyMap other)

Create a new KeyMap containing all bindings from this map followed by all bindings from other .

This map's bindings take priority (evaluated first).


The documentation for this class was generated from the following file:
  • src/ConsoleForge/Core/KeyMap.cs