Search code examples
c#consoleconsole-input

How to make the console accept input from the Enter key only?


I'd like to make the C# console only accept input from the Enter key on the startup screen.

I've made it so that it closes the console when anything but the Enter key is pressed.

How can I make it so that the console only accepts input from the Enter key so that the application doesn't close when I press anything else, and then receive normal input afterwards?

class Program
{
    public static void ClearKeyBuffer()
    {
        while (Console.KeyAvailable)
            Console.ReadKey(true);
    }
    public static void Main (string[] args)
    {
        int attempts = 0;
        int displayattempts = 5;
        bool validentry;
        Console.WriteLine("Please press enter to begin");
        var key = System.Console.ReadKey(true);
        if (key.Key == ConsoleKey.Enter)
        {
            while (attempts < 5)
            {
                string input;
                attempts = (attempts + 1);
                Console.Clear();
                Console.WriteLine("Please wait...");
                Thread.Sleep(5000);
                Console.Clear();
                Console.WriteLine("Please enter your user number.");
                Console.WriteLine("Attempts Remaining:" + displayattempts);
                ClearKeyBuffer();
                Console.WriteLine(" ");
                input = Console.ReadLine();
                {
                    if (input == "5573")
                    {
                        validentry = true;
                    }

                    else
                    {
                        validentry = false;
                    }

                    if (validentry == false)
                    {
                        displayattempts = (displayattempts - 1);
                        Console.Clear();
                        Console.WriteLine("Error: Invalid number ID entered. Please wait 5 
                        seconds, and try again.");
                        Thread.Sleep(5000);
                    }

                    else if (validentry == true)
                    {
                        Console.Clear();
                        Console.WriteLine("Welcome Samuel");
                        ValidUserEntry();
                    }
                }
            }
        }
        if (displayattempts == 0)
        {
            Console.Clear();
            Console.WriteLine("Error: You have entered the wrong number ID too many times. 
             This system will now close in 5 seconds...");
            Thread.Sleep(5000);
            Environment.Exit(0);
        }
    }

    public static void ValidUserEntry()
    {
        ClearKeyBuffer();
        Console.Clear();
        Console.WriteLine("Please wait...");
        Thread.Sleep(5000);
        ClearKeyBuffer();
        Console.Clear();
        Console.WriteLine("What would you like to do?");
        Console.ReadLine();
    }
}

Solution

  • Add this line before first if. Then remove if statement and the var key... line.

    while (Console.ReadKey(true).Key != ConsoleKey.Enter);
    

    Alternative, more verbose version:

    ConsoleKeyInfo key;
    do
    {
        key = Console.ReadKey(true);
    } while (key.Key != ConsoleKey.Enter);