C# Console KeyAvailable
Description
Console KeyAvailable
gets a value indicating whether
a key press is available in the input stream.
Syntax
Console.KeyAvailable
has the following syntax.
public static bool KeyAvailable { get; }
Example
The following example demonstrates how to use the KeyAvailable property to create a loop that runs until a key is pressed.
using System;/* w w w . j a v a 2 s.c o m*/
using System.Threading;
class Sample
{
public static void Main()
{
ConsoleKeyInfo cki = new ConsoleKeyInfo();
do {
Console.WriteLine("\nPress a key to display; press the 'x' key to quit.");
while (Console.KeyAvailable == false)
Thread.Sleep(250); // Loop until input is entered.
cki = Console.ReadKey(true);
Console.WriteLine("You pressed the '{0}' key.", cki.Key);
} while(cki.Key != ConsoleKey.X);
}
}