Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#의 Console.KeyAvailable() 속성

<시간/>

C#의 Console.KeyAvailable() 속성은 입력 스트림에서 키 누름이 가능한지 여부를 나타내는 값을 가져오는 데 사용됩니다.

구문

구문은 다음과 같습니다 -

public static bool KeyAvailable { get; }

이제 C#에서 Console.KeyAvailable() 속성을 구현하는 예를 살펴보겠습니다 -

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);
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

C#의 Console.KeyAvailable() 속성

이제 C#에서 Console.KeyAvailable() 속성을 구현하는 또 다른 예를 살펴보겠습니다. -

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);
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

C#의 Console.KeyAvailable() 속성