ConsoleForge 0.3.0
Elm-architecture TUI framework for .NET 8
Loading...
Searching...
No Matches
ConsoleForge.Core.KeyPattern(ConsoleKey? Key=null, bool? Shift=null, bool? Alt=null, bool? Ctrl=null, char? Character=null) Struct Reference

Pattern for matching keyboard events. More...

Public Member Functions

bool Matches (KeyMsg msg)
 Returns true if msg matches this pattern.

Static Public Member Functions

static KeyPattern Of (ConsoleKey key)
 Match the key with any modifier combination.
static KeyPattern OfChar (char c)
 Match a printable character, whatever key produced it — the layout-independent way to bind ?, /, or a case-sensitive letter.
static KeyPattern Plain (ConsoleKey key)
 Match the key with no modifiers pressed.
static KeyPattern WithAlt (ConsoleKey key)
 Match Alt + key (Shift and Ctrl are wildcards).
static KeyPattern WithCtrl (ConsoleKey key)
 Match Ctrl + key (Shift and Alt are wildcards).
static KeyPattern WithShift (ConsoleKey key)
 Match Shift + key (Alt and Ctrl are wildcards).

Detailed Description

Pattern for matching keyboard events.

Every field is optional, and null means "wildcard" — that field is not consulted. A pattern matches a KeyMsg when all of its non-null fields agree with it.

There are two ways to name a key. Of(ConsoleKey) matches the ConsoleKey, which is right for keys that produce no character — arrows, Enter, Tab, function keys. OfChar(char) matches the character the terminal actually produced, which is right for printable bindings because it is independent of keyboard layout: ? sits on a different physical key on a German or French layout, but it is still '?'.

A pattern with no field set — default(KeyPattern) — matches every key event. That is usable as a catch-all at the end of a KeyMap, but because this is a struct, a default reached by accident will silently swallow all input.

KeyPattern.Of(ConsoleKey.Enter) // Enter, any modifiers
KeyPattern.OfChar('?') // '?' on any keyboard layout
KeyPattern.OfChar('N') // capital N only — case-sensitive
KeyPattern.WithCtrl(ConsoleKey.S) // Ctrl+S
new KeyPattern(ConsoleKey.Tab, Shift: true) // Shift+Tab
Pattern for matching keyboard events.
Definition KeyPattern.cs:36
static KeyPattern OfChar(char c)
Match a printable character, whatever key produced it — the layout-independent way to bind ?...
Definition KeyPattern.cs:68
static KeyPattern WithCtrl(ConsoleKey key)
Match Ctrl + key (Shift and Alt are wildcards).
Definition KeyPattern.cs:71
static KeyPattern Of(ConsoleKey key)
Match the key with any modifier combination.
Definition KeyPattern.cs:51

Member Function Documentation

◆ Matches()

bool ConsoleForge.Core.KeyPattern.Matches ( KeyMsg msg)

Returns true if msg matches this pattern.

Null fields are skipped, so a pattern constrains only what it names.

◆ OfChar()

KeyPattern ConsoleForge.Core.KeyPattern.OfChar ( char c)
static

Match a printable character, whatever key produced it — the layout-independent way to bind ?, /, or a case-sensitive letter.

Ctrl and Alt must be absent: Ctrl+letter arrives as a control character rather than the letter, and Alt+key is a distinct binding that would otherwise match here, since the Alt path preserves KeyMsg.Character. Shift is deliberately left a wildcard — it has already been consumed producing the glyph, so requiring it would re-introduce the layout assumption this factory removes.

Control keys carry characters too (Enter is '\r', Tab '\t', Space ' '), so prefer Of(ConsoleKey) for those.


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