0

i tried :

while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape))
{
// do something
}

this not help me it didn't do what i need .. my code is :

Console.WriteLine("Please enter your Login Details:");
                    Console.Write("User Name: ");                                                               
                    string userName = Console.ReadLine();
                    Console.Write("Password: ");
                    string Password = Console.ReadLine();

                    if (cmd.Login(userName, Password))                                                          
                    {
// my rest of my code 
}

what i need in any console.readline() if i pressed ESC go to first code or start to put username and password ... i need to press ESC in my app it restart to the first stage at any time and any stage .. is this possible ?

kareem.khalil
  • 141
  • 1
  • 11
  • 2
    Possible duplicate of [Listen for key press in .NET console app](http://stackoverflow.com/questions/5891538/listen-for-key-press-in-net-console-app) – KidCode Jan 29 '16 at 20:38
  • [Check this question out!](http://stackoverflow.com/questions/5891538/listen-for-key-press-in-net-console-app) – KidCode Jan 29 '16 at 20:39
  • 1
    Read the entire question! He's not asking how to pause until a key is pressed. He's asking how he can capture a key press while he's in a `Read` or `ReadLine`, so that if the user presses ESC the `Read` is aborted and he can branch back to the beginning of the login process. – Jim Mischel Jan 29 '16 at 23:01

2 Answers2

1

Console.ReadKey() can be used to listen for specific key presses, such as the Escape key.

You may need to change your approach to read the username and password input using a method other than Console.ReadLine(). See this question: Using ReadLine() and ReadKey() Simultaneously

Community
  • 1
  • 1
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
1

The short answer is that you can't do that if you're using Console.Read or Console.ReadLine to get input. The problem is that Read and ReadLine are blocking calls that handle line editing and such (backspace, delete, left and right movement, etc.) It's buffered input. And the only way out of those is either to press Enter or to kill the program. Read and ReadLine don't do any special handling of Escape.

The only way I know of to do what you're asking is to use raw console I/O. That is, using Console.ReadKey to read each individual key and display it, and also handle backspace, end of line, etc. It's a real pain in the neck and difficult to get right.

The code below comes close to what you want to do. Basically, if the user enters a blank value for name or password, then it will go back to the top. The ESC key will clear a field. So if at the Password prompt the user types "foO" and then hits ESC, the field will be emptied. He then hits Enter and it will take him back to the beginning (i.e. Enter name).

It's not exactly what you asked for, but it's probably the best you're going to get without a whole lot of effort.

    private void DoIt()
    {
        string name;
        string pw;
        bool done = false;
        do
        {
            Console.WriteLine();
            Console.WriteLine("LOGIN");
            Console.WriteLine();
            Console.Write("User name: ");
            name = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(name))
            {
                continue;
            }

            Console.Write("Password: ");
            pw = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(pw))
            {
                continue;
            }
            done = true;
        } while (!done);

        Console.WriteLine("Logging in ...");
    }
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351