This may help you:
Console.WriteLine("Press any key to stop"); do { while (! Console.KeyAvailable) { // Do something } } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
If you want to use it in if , you can try the following:
ConsoleKeyInfo cki; while (true) { cki = Console.ReadKey(); if (cki.Key == ConsoleKey.Escape) break; }
For any key is very simple: remove if .
As @DawidFerenczy mentioned, we should note that Console.ReadKey() blocking. It stops execution and waits until a key is pressed. Depending on the context, this may (not) be convenient.
If you don't need to block execution, just check out Console.KeyAvailable . It will contain true if the key is pressed, otherwise false .
IonicΔ BizΔu
source share