Ari
Ari

Reputation: 1036

Console.ReadLine() alternative

I have the following console application:

private static bool run = false;

static void Main(string[] args)
{
    int choice = 0;  

    while (!run) 
    {
        Console.WriteLine("\n\t\t Press '1', '2' or '3' to continue");

        choice = int.Parse(Console.ReadLine());

        switch (choice)
        {
            case 1:
                {
                    Console.Clear();
                    Console.WriteLine("\n\t\t you pressed 1");
                }
            case 2:
                {
                    Console.Clear();
                    Console.WriteLine("\n\t\t you pressed 2");
                }
            case 3:
                {
                    Console.Clear();
                    Console.WriteLine("\n\t\t you pressed 3");
                }
            default:
                {
                    Console.Clear();
                    Console.WriteLine("\n\t\t Invalid key");
                    break;
                }
        }
    }

    Console.ReadLine();
}

However, with this code:

choice = int.Parse(Console.ReadLine()); 

I wish to have it so that when the user pressed the 1 key or 2 or 3, the appropriate code is executed straight away, as opposed to having to press the enter key once you've pressed the desired key. What are some alternatives?

Any help/guidance/tips is greatly appreciated, Thanks

Upvotes: 1

Views: 1655

Answers (1)

brunnerh
brunnerh

Reputation: 185553

How about ReadKey then?

Upvotes: 12

Related Questions