คุณสมบัติ Console.KeyAvailable() ใน C # ใช้เพื่อรับค่าที่ระบุว่ามีการกดปุ่มในสตรีมอินพุตหรือไม่
ไวยากรณ์
ไวยากรณ์มีดังนี้ −
public static bool KeyAvailable { get; } ตัวอย่าง
ให้เราดูตัวอย่างการใช้งานคุณสมบัติ Console.KeyAvailable() ใน C# -
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);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

ตัวอย่าง
ให้เราดูตัวอย่างอื่นเพื่อใช้คุณสมบัติ Console.KeyAvailable() ใน C# -
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);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
