← Back to Home • Next: KeyPress — Styles →
How the KeyPress control behaves while it is running: the single-keystroke wait, how valid keys
are matched, the invalid-key message, the tooltip, and aborting.
Press a valid key ← prompt
A, Ctrl+B, N(Off), Y(On) ← description (optional)
Esc:Abort.Ctrl+F1:Show/hide tooltip ← tooltip — does NOT list the valid keys, only hints
Invalid key 'Z'. Try A, Ctrl+B, N or Y. ← error line, only after a rejected key (renders AFTER the tooltip)
Every region can be recolored — see Styles.
Unlike text controls, KeyPress does not wait for Enter. It returns the instant an accepted key
is pressed:
- With no valid keys registered, the very first key the user presses ends the wait and is
returned in
.Content. - With one or more valid keys registered, only an accepted key ends the wait; every other key is rejected (see below) and the control keeps waiting.
// "Press any key to continue" — any key ends the wait
PromptPlus.Controls.KeyPress("Press any key to continue").Run();Each AddValidKey call accumulates one accepted combination. A key is
accepted only when both its ConsoleKey and the required modifiers match a registered entry:
PromptPlus.Controls.KeyPress("Press a valid key", "A, Ctrl+B, N(Off), Y(On)")
.AddValidKey(ConsoleKey.A) // A alone
.AddValidKey(ConsoleKey.B, ConsoleModifiers.Control) // requires Ctrl held
.AddValidKey(ConsoleKey.N, null, "Off") // N, labelled "Off"
.AddValidKey(ConsoleKey.Y, null, "On") // Y, labelled "On"
.Run();- No
AddValidKeycalls → any key is accepted. This is the "press any key" mode. - With a modifier requirement, the modifier must be held:
AddValidKey(ConsoleKey.B, ConsoleModifiers.Control)accepts Ctrl+B, not a bare B. - The optional
displayTextchanges what's shown on the Answer line once that key is pressed (not the tooltip — the tooltip never lists the valid keys at all, see Tooltip below). The returned.Content.Value.Keyis still the realConsoleKeyeither way.
💡 To branch on the result, compare
.Content.Value.Keyto theConsoleKeyyou registered, e.g.if (result.Content is { } k && k.Key == ConsoleKey.Y).
When valid keys are registered and the user presses one that is not accepted:
- The control stays open (the wait continues).
- If a message callback is configured, its text is shown on the error line, styled with
KeyPressStyles.Error. - The message clears when the next key is pressed.
Set the text synchronously with ShowMessage or asynchronously with
ShowMessageAsync; both receive the rejected ConsoleKeyInfo:
PromptPlus.Controls.KeyPress("Press a valid key")
.AddValidKey(ConsoleKey.A)
.AddValidKey(ConsoleKey.Y, null, "On")
.ShowMessage(key => $"Invalid key '{key.Key}'. Try A or Y.")
.Run();If no message callback is set, a rejected key is simply ignored and the control keeps waiting silently.
Esc:Abort.Ctrl+F1:Show/hide tooltip), regardless of how many keys
you registered with AddValidKey or what displayText you gave them. Toggle it per instance with
Options(o => o.ShowTooltip(...)) or globally via
PromptPlus.Config.
| Key | Action |
|---|---|
F1 |
Cycle tooltip content |
Ctrl+F1 |
Show / hide the tooltip |
When the abort key is enabled (the default), pressing Esc cancels the wait:
.IsAbortedistrue..Contentdoes have a value — the real Escape key'sConsoleKeyInfo(.Content.Value.Key == ConsoleKey.Escape)..Contentis only genuinelynull/no-value when the wait ends via theCancellationTokenpassed toRun(...), not via Esc.- The Answer line switches to a localized "Canceled" label (instead of whatever key text would
normally show) when
ShowMessageAbortKeyistrue(the default).
var result = PromptPlus.Controls.KeyPress("Press a key").Run();
if (result.IsAborted)
PromptPlus.Console.WriteLine("Cancelled.");
else
PromptPlus.Console.WriteLine($"Pressed {result.Content!.Value.Key}");Disable Esc with Options(o => o.EnabledAbortKey(false)) to force the user to
press a valid key.
Set per instance via Options(...), or globally on
PromptPlus.Config:
| Option | Effect on KeyPress |
|---|---|
EnabledAbortKey(false) |
Removes Esc — the user must press a valid key |
HideAfterFinish(true) |
Erases the prompt after a key is pressed |
HideOnAbort(true) |
Erases the prompt after Esc |
ShowTooltip(false) |
Hides the key-hint line |
Prompt(...) / Description(...) |
Overrides the prompt / description text |
The
showresultfactory parameter is the sibling switch for whether the pressed-key answer line remains visible after the control finishes.
.Contentis nullable, but an Esc-abort still populates it with the real EscapeConsoleKeyInfo— only aCancellationTokencancel leaves itnull. Branch on.IsAborted, not on.Content.HasValue, to detect an abort.- Modifiers matter. A registered
Ctrl+Bwill not match a bareB, and vice versa. - Message callbacks block the UI thread — see the warning under
ShowMessageAsync.
- Methods — the API these behaviors come from
- Styles — the
KeyPressStylesregions - Confirm → Operations — the yes/no interpretation of the same flow
- Global Behaviors — the config layer behind
Options