← Back to Home • Next: KeyPress — Methods →
Wait for a single keystroke. The user presses one key and the control returns immediately — no Enter required.
The KeyPress control is the primitive for “press any key to continue” pauses and for
single-key menus (press A, B, or C). By default any key satisfies it; register one or
more valid keys and it keeps waiting until the user presses an accepted key (or combination),
showing an optional message for every rejected key. Everything is configured through a single
fluent chain.
✅ Need a plain yes/no question? Use the Confirm control — it is the same
IKeyPressControlwith the culture-specific Yes/No keys already registered.
| Sub-page | What you will find |
|---|---|
| Index (this page) | What it is, when to use it, a first working example, the method map |
| Methods | Every fluent method — signature, parameters, defaults, and a snippet |
| Operations | Keyboard, valid-key matching, invalid-key message, tooltip, abort |
| Styles | The KeyPressStyles regions and how to recolor them |
Use KeyPress when… |
Consider instead… |
|---|---|
| You want a “press any key to continue” pause | — |
| You want a single-key choice (A / B / C, 1 / 2) | — |
| You need a yes/no answer | Confirm |
| The user should pick from a labelled list | Select |
| You need free-form typed text | Input |
using PromptPlusLibrary;
var result = PromptPlus.Controls
.KeyPress("Press any key to continue")
.Run();
if (!result.IsAborted && result.Content.HasValue)
PromptPlus.Console.WriteLine($"You pressed {result.Content.Value.Key}");
KeyPress("Press any key to continue") creates the control. The first argument is the
prompt; an optional second argument is a description line shown under it..Run() renders the prompt and blocks until the user presses a key (or Esc to abort).ResultPrompt<ConsoleKeyInfo?>: read
.Content for the key that was pressed and .IsAborted to detect Esc.⚠️
.Contentis a nullableConsoleKeyInfo?, but pressing Esc to abort does not leave it null —.Contentgets the real Escape key’sConsoleKeyInfo(so.Content.HasValueis stilltrue, and.Content.Value.Key == ConsoleKey.Escape)..Contentis only genuinelynullwhen the wait is ended by cancelling theCancellationTokenpassed toRun(...), a different case from a user pressing Esc. Always check.IsAbortedfirst — don’t rely on.Content.HasValuealone to detect an abort.
using PromptPlusLibrary;
var choice = PromptPlus.Controls
.KeyPress("Press a valid key", "A, Ctrl+B, N(Off), Y(On)")
.AddValidKey(ConsoleKey.A)
.AddValidKey(ConsoleKey.B, ConsoleModifiers.Control)
.AddValidKey(ConsoleKey.N, null, "Off")
.AddValidKey(ConsoleKey.Y, null, "On")
.ShowMessage(key => $"Invalid key '{key.Key}'. Try A, Ctrl+B, N or Y.")
.Run();
if (!choice.IsAborted && choice.Content is { } key)
PromptPlus.Console.WriteLine($"Accepted {key.Key}");
This shows the three building blocks together: restricting input (each
AddValidKey accumulates one accepted combination), an optional
display label for the tooltip, and a message for rejected keys
(ShowMessage). See Operations for how they interact.
Grouped by purpose. Full signatures and examples are on the Methods page.
| Purpose | Methods |
|---|---|
| Restrict which keys are accepted | AddValidKey |
| Message for rejected keys | ShowMessage, ShowMessageAsync |
| Appearance & behavior | Styles, Options |
| Run | Run |
KeyPress returns ResultPrompt<ConsoleKeyInfo?>.
| Member | Meaning |
|---|---|
.Content |
The pressed key as a nullable ConsoleKeyInfo?. On Esc-abort this is still the real Escape ConsoleKeyInfo, not null — only a CancellationToken cancel yields null |
.IsAborted |
true when the user pressed Esc / the abort key |
var result = PromptPlus.Controls.KeyPress("Continue?").Run();
if (!result.IsAborted && result.Content.HasValue)
{
var info = result.Content.Value;
PromptPlus.Console.WriteLine($"Key={info.Key}, Char={info.KeyChar}");
}
The result is a
ConsoleKeyInfo?, not a bool — it carries the physical key (.Key), the character (.KeyChar), and any held modifiers (.Modifiers).