Share via


C# Console application, getting input without displaying it

Question

Thursday, December 26, 2013 11:46 AM

I want to receive input of a single char from the user without having the inputted character actually appear on the screen.

Right now I use this:

pick = '0';
pickint = 0;
picked = false;
while (!picked)
{
     pick = Console.ReadKey().KeyChar;
     Console.WriteLine();
     if ((int.TryParse(pick.ToString(), out pickint)) && (pickint > 0 && pickint <= picks))
           picked = true;
}

The result is:

"

__You are in place A___

1. Do this.

2. Do that.

2 (<-- this is the user input)

You do that.

"

I want to do this but without the 2 the user types appearing on screen, how can I do this?

All replies (2)

Thursday, December 26, 2013 12:38 PM âś…Answered | 1 vote

Hi, you can use Console.ReadKey(true) to intercept the key press and not display it in the console window.

pick = Console.ReadKey(true).KeyChar;

Thursday, December 26, 2013 8:08 PM

Thanks, it worked.