Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 756
user3002135 Avatar asked Dec 30 '13 19:12

user3002135


2 Answers

Try this:

ConsoleKeyInfo input;
do
{
    input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);
like image 165
Kevin Brechbühl Avatar answered Oct 07 '22 06:10

Kevin Brechbühl


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!
    }
like image 24
Erik Avatar answered Oct 07 '22 07:10

Erik