Console.KeyAvailable() Property in C#
Console.KeyAvailable Property is used to get a value which shows whether a key press is available in the input stream. This property does not block input until a key press is available.
Syntax: public static bool KeyAvailable { get; }
Property Value: It will return true if a key press is available otherwise, it retuns false.
Exceptions:
- InvalidOperationException: If the standard input is redirected to a file instead of the keyboard.
- IOException: If an I/O error occured.
Example:
// C# Program to demostrate // KeyAvailable property using System; using System.Threading; namespace GFG { class GFG { public static void Main() { // declare a new ConsoleKeyInfo object ConsoleKeyInfo c = new ConsoleKeyInfo(); // outer loop to work until 'z' is pressed do { Console.WriteLine("\nPress a key to display; "+ "press the 'z' key to quit."); // inner loop to check whether a key // is pressed using KeyAvailable while (Console.KeyAvailable == false) // Loop until input is entered. Thread.Sleep(50); c = Console.ReadKey(true); Console.WriteLine("You pressed the '{0}' key.", c.Key); } while (c.Key != ConsoleKey.Z); } } } |
chevron_right
filter_none
Reference:
Recommended Posts:
- C# | SortedDictionary.Values Property
- C# | SortedDictionary.Keys Property
- C# | SortedDictionary.Count Property
- C# | Dictionary.Item[] Property
- C# | Dictionary.Count Property
- C# | Dictionary.Values Property
- C# | SortedDictionary.Item[] Property
- C# | Dictionary.Keys Property
- How to use AcceptsReturn Property of TextBox in C#?
- How to use AutoCheck property of CheckBox in C#?
- Queue.IsSynchronized Property in C#
- Queue.Count Property in C#
- Stack.Count Property in C#
- Stack.IsSynchronized Property in C#
- C# | LongLength property of an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




