I\'m using Console.ReadLine
to read the input of the user. However, I want to hide/exclude the inputted text on the console screen while typing. For example, whe
I created this method from the code in the answer by dataCore and the suggestion by m4tt1mus. I also added support for the backspace key.
private static string GetHiddenConsoleInput()
{
StringBuilder input = new StringBuilder();
while (true)
{
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter) break;
if (key.Key == ConsoleKey.Backspace && input.Length > 0) input.Remove(input.Length - 1, 1);
else if (key.Key != ConsoleKey.Backspace) input.Append(key.KeyChar);
}
return input.ToString();
}