I have a tiny command line utility which reads user input and adds all input to a list. When the Esc key is pressed, the program is quit. However, upon pressing the Esc key, even the Escape key is being added to list and then the program quits.
How can I prevent the Esc key from being added to the list?
Code:
ConsoleKeyInfo cki;
List<string> stuffs = new List<string>();
Console.WriteLine("Press Escape (Esc) to quit the program.");
do {
cki = Console.ReadKey();
stuffs.Add(cki.Key.ToString());
Console.WriteLine();
} while(cki.Key != ConsoleKey.Escape);
Console.WriteLine("You have entered the following data:");
foreach(string stuff in stuffs)
Console.WriteLine(stuff);
Change the loop so that you check the value prior to adding it to your list. For example, this loops the same, but breaks when Escape is pressed: