คุณสมบัติ Console.KeyAvailable ใน C# ใช้สำหรับตรวจสอบว่ามีการกดปุ่มพร้อมใช้งานในสตรีมอินพุตหรือไม่
ไวยากรณ์
public static bool KeyAvailable { get; }ตัวอย่าง
using System;
using System.Threading;
class Demo {
public static void Main (string[] args) {
ConsoleKeyInfo cs = new ConsoleKeyInfo();
do {
Console.WriteLine("\nPress a key to display; " + "press the 'Q' key to quit.");
while (Console.KeyAvailable == false) Thread.Sleep(100);
cs = Console.ReadKey(true);
Console.WriteLine("You pressed the '{0}' key.", cs.Key);
} while (cs.Key != ConsoleKey.Q);
}
}ผลลัพธ์

ตัวอย่างที่ 2
using System;
using System.Threading;
class Demo {
public static void Main (string[] args) {
ConsoleKeyInfo cs = new ConsoleKeyInfo();
do {
Console.WriteLine("\nPress a key to display; " + "press the 'P' key to quit.");
while (Console.KeyAvailable == false) Thread.Sleep(200);
cs = Console.ReadKey(true);
Console.WriteLine("You pressed the '{0}' key.", cs.Key);
} while (cs.Key != ConsoleKey.Q);
}
}ผลลัพธ์
