Search code examples
c#consoleconsole-applicationconsole.readkeyreadkey

ReadKey while key is not pressed do something


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?


Solution

  • Try this:

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