PromptPlus

PromptPlus # PromptPlus ## **KeyPress** [![NuGet](https://img.shields.io/badge/NuGet-PromptPlus-blue)](https://www.nuget.org/packages/PromptPlus) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![.NET](https://img.shields.io/badge/.NET-8%20%7C%209%20%7C%2010-512BD4)](https://dotnet.microsoft.com/)

← Back to HomeNext: 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 IKeyPressControl with the culture-specific Yes/No keys already registered.


On this page

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

When to use it

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

Minimal example

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}");

⚠️ .Content is a nullable ConsoleKeyInfo?, but pressing Esc to abort does not leave it null — .Content gets the real Escape key’s ConsoleKeyInfo (so .Content.HasValue is still true, and .Content.Value.Key == ConsoleKey.Escape). .Content is only genuinely null when the wait is ended by cancelling the CancellationToken passed to Run(...), a different case from a user pressing Esc. Always check .IsAborted first — don’t rely on .Content.HasValue alone to detect an abort.


A more complete example

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.


Method map

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

Return value

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).


See also