How to read "Enter" from the keyboard to exit program
Asked Answered
R

5

9

I have written a simple program in C# in Visual Studio 2013. At the end of my program I instruct the user to:

"Please Press Enter to Exit the Program."

I would like to get the input from the keyboard on the next line and if ENTER is pressed, the program will quit.

Can anyone tell me how I can achieve this function?

I have tried the following code:

Console.WriteLine("Press ENTER to close console......");
String line = Console.ReadLine();

if(line == "enter")
{
    System.Environment.Exit(0);
}
Rudbeckia answered 18/9, 2015 at 15:34 Comment(3)
Maybe a trivial answer but Console.ReadLine() doesn't solve the problem? What do you need?Septicemia
@RezaAghaei I updated the question with the code I have tried.Rudbeckia
The answer is Console.Readline()Septicemia
F
10

Try following:

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

You can use a do-while too. More informations: Console.ReadKey()

Ferromagnetism answered 18/9, 2015 at 15:45 Comment(0)
J
8

Use Console.ReadKey(true); like this:

ConsoleKeyInfo keyInfo = Console.ReadKey(true); //true here mean we won't output the key to the console, just cleaner in my opinion.
if (keyInfo.Key == ConsoleKey.Enter)
{
    //Here is your enter key pressed!
}
Jackquelinejackrabbit answered 18/9, 2015 at 15:46 Comment(0)
S
7

If you write the Program this way:

  • You don't need to call System.Environment.Exit(0);
  • Also you don't need to check for input key.

Example:

class Program
{
    static void Main(string[] args)
    {
        //....
        Console.WriteLine("Press ENTER to exit...");
        Console.ReadLine();
    }
}

Another Example:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Press Enter in an emplty line to exit...");
        var line= "";
        line = Console.ReadLine();
        while (!string.IsNullOrEmpty(line))
        {
            Console.WriteLine(string.Format("You entered: {0}, Enter next or press enter to exit...", line));
            line = Console.ReadLine();
        }
    }
}

Yet Another Example:

If you need, you can check if the value read by Console.ReadLine() is empty, then Environment.Exit(0);

//...
var line= Console.ReadLine();
if(string.IsNullOrEmpty(line))
    Environment.Exit(0)
else
    Console.WriteLine(line);
//...
Septicemia answered 18/9, 2015 at 15:43 Comment(0)
B
1
Console.WriteLine("Press Enter");
if (Console.ReadKey().Key == ConsoleKey.Enter)
{
    Console.WriteLine("User pressed \"Enter\"");
}
Banc answered 16/11, 2021 at 18:12 Comment(0)
D
0

In a more functional approach you can use a loop and read the key to use multiple options until the specific key is pressed to exit the console I used key number 0 (ConsoleKey.NumPad0) you can change that for Enter (ConsoleKey.Enter)

Console.WriteLine("Write Option: 1 ~ 3 or 0 to Exit.");
while (true)
{
    ConsoleKeyInfo keyInfo = Console.ReadKey(true);
    // Use if because switch will not allow break.
    if (keyInfo.Key == ConsoleKey.NumPad0) 
    {
        break; // Exit the loop.
    }
    switch (keyInfo.Key) 
    {
        case ConsoleKey.NumPad1:
            Console.WriteLine("Option 1 Selected");
        break;
        case ConsoleKey.NumPad2:
            Console.WriteLine("Option 2 Selected");
        break;
        case ConsoleKey.NumPad3:
            Console.WriteLine("Option 3 Selected");
            break;
        default: Console.WriteLine("Please enter a valid option."); break;
    }
}
Drud answered 19/9, 2023 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.