Masked Input
MaskedInputLineReader reads sensitive console input with * masking. The cursor must be at column 0 when ReadMaskedInput is called, or it throws InvalidOperationException. Write your prompt with a trailing newline (e.g. Console.WriteLine) so the cursor lands at column 0.
Input can span multiple terminal rows and is capped at 65,536 characters (64K); past that, ReadMaskedInput throws InvalidOperationException.
The reader holds no console state at construction and resets on each call, so a single instance can be reused for multiple reads (e.g. a password-retry loop).
cmdDef.Handler = (cmd) =>
{
Console.WriteLine("Enter password:");
var reader = new MaskedInputLineReader();
string password = reader.ReadMaskedInput();
if (password is null)
return; // cancelled: Escape or console resize
// use password...
};| Key | Behavior |
|---|---|
Enter | Submit input |
Escape | Cancel: clears the line, returns null |
↑ / ↓ | Toggle mask visibility |
← / → | Move cursor |
Home / End | Jump to start / end |
Backspace / Delete | Delete character before / at cursor |
Insert | Toggle insert / overwrite mode |
A console resize during
ReadMaskedInput also cancels the read and returns null. Cursor tracking is unreliable once the terminal width changes mid-read.