Thuộc tính Console.KeyAvailable () trong C # được sử dụng để nhận một giá trị cho biết liệu một lần nhấn phím có khả dụng trong luồng đầu vào hay không.
Cú pháp
Cú pháp như sau -
public static bool KeyAvailable { get; } Ví dụ
Bây giờ chúng ta hãy xem một ví dụ để triển khai thuộc tính Console.KeyAvailable () trong 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);
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
Ví dụ
Bây giờ chúng ta hãy xem một ví dụ khác để triển khai thuộc tính Console.KeyAvailable () trong 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);
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -