ReadKey while key is not pressed do something

user3002135 picture user3002135 · Dec 30, 2013 · Viewed 11.8k times · Source

I am trying to run my code until Esc was pressed. Therefore I am using ReadKey in my Console

var input = Console.ReadKey();
do
{

} while (input.Key != ConsoleKey.Escape);

but at "ConsoleKey" it says that, ConsoleKey isn't possible in 'bool'. How can I fix that? Or what shall I use instead?

Answer

Kevin Brechbühl picture Kevin Brechbühl · Dec 30, 2013

Try this:

ConsoleKeyInfo input;
do
{
    input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);