Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Console.KeyAvailable() Property in C#
The Console.KeyAvailable property in C# is used to check whether a key press is available in the input buffer without blocking the thread. It returns true if a key has been pressed and is waiting to be read, or false if no key press is pending.
This property is particularly useful for creating non-blocking input operations where your program can continue executing other tasks while occasionally checking for user input.
Syntax
Following is the syntax for the Console.KeyAvailable property −
public static bool KeyAvailable { get; }
Return Value
The property returns a bool value −
-
true− A key press is available in the input buffer -
false− No key press is waiting to be read
Using KeyAvailable for Non-Blocking Input
The primary advantage of KeyAvailable is that it allows your program to check for input without pausing execution. This is useful for interactive applications that need to respond to user input while performing other operations −
using System;
using System.Threading;
class Demo {
public static void Main(string[] args) {
ConsoleKeyInfo keyInfo;
int counter = 0;
Console.WriteLine("Program running... Press any key to see it detected, 'Q' to quit.");
do {
// Simulate other work being done
Thread.Sleep(500);
counter++;
if (Console.KeyAvailable) {
keyInfo = Console.ReadKey(true);
Console.WriteLine($"Key detected: '{keyInfo.Key}' (Counter: {counter})");
if (keyInfo.Key == ConsoleKey.Q) {
break;
}
} else {
Console.Write(".");
}
} while (true);
Console.WriteLine("\nProgram ended.");
}
}
The output shows the program continuing to work (printing dots) while detecting key presses when they occur −
Program running... Press any key to see it detected, 'Q' to quit. ......Key detected: 'A' (Counter: 7) .....Key detected: 'B' (Counter: 13) ....Key detected: 'Q' (Counter: 17) Program ended.
Using KeyAvailable with Input Validation
Here's an example that demonstrates using KeyAvailable for menu-driven applications −
using System;
using System.Threading;
class MenuDemo {
public static void Main(string[] args) {
Console.WriteLine("Menu: Press 1, 2, or 3 for options. Press 'E' to exit.");
Console.WriteLine("Program will show a timer while waiting for input...");
int seconds = 0;
ConsoleKeyInfo key;
do {
Thread.Sleep(1000);
seconds++;
Console.Write($"\rWaiting... {seconds} seconds ");
if (Console.KeyAvailable) {
key = Console.ReadKey(true);
Console.WriteLine();
switch (key.KeyChar) {
case '1':
Console.WriteLine("Option 1 selected!");
break;
case '2':
Console.WriteLine("Option 2 selected!");
break;
case '3':
Console.WriteLine("Option 3 selected!");
break;
case 'e':
case 'E':
Console.WriteLine("Exiting program...");
return;
default:
Console.WriteLine($"Invalid option: '{key.KeyChar}'");
break;
}
seconds = 0; // Reset timer after input
}
} while (true);
}
}
The output demonstrates how the timer continues running while waiting for valid menu selections −
Menu: Press 1, 2, or 3 for options. Press 'E' to exit. Program will show a timer while waiting for input... Waiting... 5 seconds Option 1 selected! Waiting... 3 seconds Invalid option: 'x' Waiting... 2 seconds Exiting program...
Common Use Cases
- Game Loops − Check for player input while updating game state
- Real-time Applications − Monitor for commands while processing data
- Interactive Menus − Provide responsive user interfaces
- Background Tasks − Allow user interruption of long-running operations
Conclusion
The Console.KeyAvailable property is essential for creating responsive console applications that can handle user input without blocking program execution. It enables non-blocking input checking, making it perfect for interactive programs, games, and real-time applications that need to balance user responsiveness with continuous processing.
