5

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?

Flimm
  • 136,138
  • 45
  • 251
  • 267
user3002135
  • 237
  • 1
  • 4
  • 15
  • There is no such error with that code. It'll loop forever, as it stands, but it'll compile just fine. If you read a new key in the body of the loop then it'll even work correctly. – Servy Dec 30 '13 at 19:19
  • hm it gives an error for me... i probably forgot something in the code... – user3002135 Dec 30 '13 at 19:20
  • The last paragraph of this post was unclear and looked like a completely different question. I've edited the post, feel free to create another question post, OP. – Flimm Dec 30 '13 at 20:39

3 Answers3

11

Try this:

ConsoleKeyInfo input;
do
{
    input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);
Kevin Brechbühl
  • 4,717
  • 3
  • 24
  • 47
  • cut how can i use it like, exiting my program at any point? i'm looking for closing it when ESC is pressed means it has to check all the time. i recently started into programming so i probably dont know how to run multiple threads. – user3002135 Dec 30 '13 at 19:22
  • So this is very dependent what your requirements are and what for an application you are trying to implement. – Kevin Brechbühl Dec 30 '13 at 19:26
6

Is there a particular reason you want to use the ESC key instead of the conventional CTRL+C?

You can hook into the Console.CancelKeyPress event for the latter and it is standard in the command-line interface world.

Console.ReadKey() is blocking, which can be problematic in some loops. Let's take this example:

    using System.Threading;
    using System.Threading.Tasks;

    CancellationTokenSource cts;

    public void Run()
    {
        cts = new CancellationTokenSource();
        var task = new Task(DoSomething, cts.Token);

        task.Start();

        while (!task.IsCompleted)
        {
            var keyInput = Console.ReadKey(true);

            if (keyInput.Key == ConsoleKey.Escape)
            {
                Console.WriteLine("Escape was pressed, cancelling...");
                cts.Cancel();
            }
        }

        Console.WriteLine("Done.");
    }

    void DoSomething()
    {
        var count = 0;

        while (!cts.IsCancellationRequested)
        {
            Thread.Sleep(1000);
            count++;

            Console.WriteLine("Background task has ticked ({0}).", count.ToString());
        }
    }

This will do some background work using a Task, while waiting for ESC to be pressed. Cancellation works just fine, however it will be stuck at the Console.ReadKey() one more time after completion (cancellation).

You could use Win32 API such as GetKeyboardState and check the key codes instead, since it is not blocking. However, I recommend using the CancelKeyPress event instead (CTRL+C):

    void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
    {
        Console.WriteLine("Cancelling...");
        cts.Cancel();

        e.Cancel = true;    // Do not terminate immediately!
    }
vahid abdi
  • 9,636
  • 4
  • 29
  • 35
Erik
  • 12,730
  • 5
  • 36
  • 42
3
ConsoleKeyInfo input;
do
{
    input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);

or shorter

while (Console.ReadKey().Key != ConsoleKey.Escape){}
burning_LEGION
  • 13,246
  • 8
  • 40
  • 52