← 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:
.Content.// "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();
AddValidKey calls → any key is accepted. This is the “press any key” mode.AddValidKey(ConsoleKey.B, ConsoleModifiers.Control)
accepts Ctrl+B, not a bare B.displayText changes 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.Key is still the real ConsoleKey either 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:
KeyPressStyles.Error.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.
⚠️ The tooltip line does not list the accepted keys — it only ever shows the abort hint and the
show/hide-tooltip hint (e.g. 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:
.IsAborted is true..Content does have a value — the real Escape key’s ConsoleKeyInfo
(.Content.Value.Key == ConsoleKey.Escape). .Content is only genuinely null/no-value when the
wait ends via the CancellationToken passed to Run(...), not via Esc.ShowMessageAbortKey is true (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.
.Content is nullable, but an Esc-abort still populates it with the real Escape
ConsoleKeyInfo — only a CancellationToken cancel leaves it null. Branch on .IsAborted, not
on .Content.HasValue, to detect an abort.Ctrl+B will not match a bare B, and vice versa.ShowMessageAsync.KeyPressStyles regionsOptions