Console.KeyAvailable Console.KeyAvailable Console.KeyAvailable Console.KeyAvailable Property

정의

입력 스트림에서 키 누름을 사용할 수 있는지를 나타내는 값을 가져옵니다.Gets a value indicating whether a key press is available in the input stream.

public:
 static property bool KeyAvailable { bool get(); };
public static bool KeyAvailable { get; }
member this.KeyAvailable : bool
Public Shared ReadOnly Property KeyAvailable As Boolean

속성 값

키 누름을 사용할 수 있으면 true이고, 사용할 수 없으면 false입니다.true if a key press is available; otherwise, false.

예외

I/O 오류가 발생했습니다.An I/O error occurred.

표준 입력이 키보드 대신에 파일로 리디렉션됩니다.Standard input is redirected to a file instead of the keyboard.

예제

다음 예제에서는 사용 하는 방법에 설명 합니다 KeyAvailable 키를 누를 때까지 실행 되는 루프를 만들 속성입니다.The following example demonstrates how to use the KeyAvailable property to create a loop that runs until a key is pressed.

using namespace System;
using namespace System::Threading;
int main()
{
   ConsoleKeyInfo cki;
   do
   {
      Console::WriteLine( "\nPress a key to display; press the 'x' key to quit." );
      
      // Your code could perform some useful task in the following loop. However, 
      // for the sake of this example we'll merely pause for a quarter second.
      while ( Console::KeyAvailable == false )
            Thread::Sleep( 250 );
      cki = Console::ReadKey( true );
      Console::WriteLine( "You pressed the '{0}' key.", cki.Key );
   }
   while ( cki.Key != ConsoleKey::X );
}

/*
This example produces results similar to the following:

Press a key to display; press the 'x' key to quit.
You pressed the 'H' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'E' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'PageUp' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'DownArrow' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'X' key.
*/
using System;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
       ConsoleKeyInfo cki;

       do {
           Console.WriteLine("\nPress a key to display; press the 'x' key to quit.");

           // Your code could perform some useful task in the following loop. However, 
           // for the sake of this example we'll merely pause for a quarter second.
   
           while (Console.KeyAvailable == false)
               Thread.Sleep(250); // Loop until input is entered.
           
           cki = Console.ReadKey(true);
           Console.WriteLine("You pressed the '{0}' key.", cki.Key);
        } while(cki.Key != ConsoleKey.X);
    }
}
/*
This example produces results similar to the following:

Press a key to display; press the 'x' key to quit.
You pressed the 'H' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'E' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'PageUp' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'DownArrow' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'X' key.
*/
Imports System.Threading

Class Sample
   Public Shared Sub Main()
      Dim cki As ConsoleKeyInfo
      
      Do
         Console.WriteLine(vbCrLf & "Press a key to display; press the 'x' key to quit.")

         ' Your code could perform some useful task in the following loop. However, 
         ' for the sake of this example we'll merely pause for a quarter second.
         
         While Console.KeyAvailable = False
            Thread.Sleep(250) ' Loop until input is entered.
         End While
         cki = Console.ReadKey(True)
         Console.WriteLine("You pressed the '{0}' key.", cki.Key)
      Loop While cki.Key <> ConsoleKey.X
   End Sub 
End Class 
'This example produces results similar to the following:
'
'Press a key to display; press the 'x' key to quit.
'You pressed the 'H' key.
'
'Press a key to display; press the 'x' key to quit.
'You pressed the 'E' key.
'
'Press a key to display; press the 'x' key to quit.
'You pressed the 'PageUp' key.
'
'Press a key to display; press the 'x' key to quit.
'You pressed the 'DownArrow' key.
'
'Press a key to display; press the 'x' key to quit.
'You pressed the 'X' key.
'

설명

속성 값을 즉시 반환 됩니다. 즉,는 KeyAvailable 속성 키 누름을 사용할 때까지 입력을 차단 하지 않습니다.The property value is returned immediately; that is, the KeyAvailable property does not block input until a key press is available.

사용 하 여는 KeyAvailable 만 함께에서 속성을 ReadKey 메서드를 하지는 Read 또는 ReadLine 메서드.Use the KeyAvailable property in conjunction with only the ReadKey method, not the Read or ReadLine methods.

적용 대상