Console.ReadKey Console.ReadKey Console.ReadKey Console.ReadKey Method

정의

다음 문자나 사용자가 누른 기능 키를 가져옵니다.Obtains the next character or function key pressed by the user.

오버로드

ReadKey() ReadKey() ReadKey() ReadKey()

다음 문자나 사용자가 누른 기능 키를 가져옵니다.Obtains the next character or function key pressed by the user. 누른 키는 콘솔 창에 표시됩니다.The pressed key is displayed in the console window.

ReadKey(Boolean) ReadKey(Boolean) ReadKey(Boolean) ReadKey(Boolean)

다음 문자나 사용자가 누른 기능 키를 가져옵니다.Obtains the next character or function key pressed by the user. 누른 키는 선택적으로 콘솔 창에 표시됩니다.The pressed key is optionally displayed in the console window.

ReadKey() ReadKey() ReadKey() ReadKey()

다음 문자나 사용자가 누른 기능 키를 가져옵니다.Obtains the next character or function key pressed by the user. 누른 키는 콘솔 창에 표시됩니다.The pressed key is displayed in the console window.

public:
 static ConsoleKeyInfo ReadKey();
public static ConsoleKeyInfo ReadKey ();
static member ReadKey : unit -> ConsoleKeyInfo
Public Shared Function ReadKey () As ConsoleKeyInfo

반환

누른 콘솔 키에 해당하는 ConsoleKey 상수 및 유니코드 문자(있을 경우)를 설명하는 개체입니다.An object that describes the ConsoleKey constant and Unicode character, if any, that correspond to the pressed console key. ConsoleKeyInfo 개체는 하나 이상의 Shift, Alt 또는 Ctrl 보조키를 콘솔 키와 동시에 눌렀는지 여부를 나타내는 ConsoleModifiers 값을 비트 조합으로 나타냅니다.The ConsoleKeyInfo object also describes, in a bitwise combination of ConsoleModifiers values, whether one or more Shift, Alt, or Ctrl modifier keys was pressed simultaneously with the console key.

예외

In 속성이 콘솔 이외의 다른 스트림에서 리디렉션됩니다.The In property is redirected from some stream other than the console.

예제

가장 일반적인 이유 중 하나가 사용 하는 ReadKey() 방법은 키를 누르면와 앱을 종료 하거나 정보는 추가 창이 표시 될 때까지 프로그램 실행을 중단 하는 것입니다.One of the most common uses of the ReadKey() method is to halt program execution until the user presses a key and the app either terminates or displays an additional window of information. 다음 예제에서는 ReadKey() 사용자를 앱을 종료 하기 전에 Enter 키를 누를 때까지 대기 하는 방법입니다.The following example uses the ReadKey() method to wait for the user to press the Enter key before terminating the app.

using System;

public class Example
{
   public static void Main()
   {
      DateTime dat = DateTime.Now;
      Console.WriteLine("The time: {0:d} at {0:t}", dat);
      TimeZoneInfo tz = TimeZoneInfo.Local;
      Console.WriteLine("The time zone: {0}\n", 
                        tz.IsDaylightSavingTime(dat) ?
                           tz.DaylightName : tz.StandardName);
      Console.Write("Press <Enter> to exit... ");
      while (Console.ReadKey().Key != ConsoleKey.Enter) {}
   }
}
// The example displays output like the following:
//     The time: 11/11/2015 at 4:02 PM:
//     The time zone: Pacific Standard Time
Module Example
   Public Sub Main()
      Dim dat As Date = Date.Now
      Console.WriteLine("The time: {0:d} at {0:t}", dat)
      Dim tz As TimeZoneInfo = TimeZoneInfo.Local
      Console.WriteLine("The time zone: {0}", 
                        If(tz.IsDaylightSavingTime(dat),
                           tz.DaylightName, tz.StandardName))
      Console.WriteLine()
      Console.Write("Press <Enter> to exit... ")
      Do While Console.ReadKey().Key <> ConsoleKey.Enter
      Loop
   End Sub
End Module
' The example displays the following output:
'     The time: 11/11/2015 at 4:02 PM
'     The time zone: Pacific Standard Time

이 오버 로드는 ReadKey 기본적으로 메서드를 누르면 콘솔에 표시할 수 있는 모든 키는 에코 합니다.Note that this overload of the ReadKey method by default echoes any displayable keys that the user presses to the console. 경고를 호출 합니다 ReadKey 메서드는 intercept 인수의 true합니다.To suppress them, call the ReadKey method with an intercept argument of true.

다음 예제에서는 ReadKey() 메서드를 사용 하 여 사용자가 누른 키에 대 한 정보를 표시 합니다.The following example uses the ReadKey() method to display information about which key the user pressed.

using namespace System;

void main()
{
   ConsoleKeyInfo cki;
   // Prevent example from ending if CTL+C is pressed.
   Console::TreatControlCAsInput = true;

   Console::WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
   Console::WriteLine("Press the Escape (Esc) key to quit: \n");
   do 
   {
      cki = Console::ReadKey();
      Console::Write(" --- You pressed ");
      if((cki.Modifiers & ConsoleModifiers::Alt) != ConsoleModifiers()) Console::Write("ALT+");
      if((cki.Modifiers & ConsoleModifiers::Shift) != ConsoleModifiers()) Console::Write("SHIFT+");
      if((cki.Modifiers & ConsoleModifiers::Control) != ConsoleModifiers()) Console::Write("CTL+");
      Console::WriteLine(cki.Key.ToString());
   } while (cki.Key != ConsoleKey::Escape);
}
// This example displays output similar to the following:
//       Press any combination of CTL, ALT, and SHIFT, and a console key.
//       Press the Escape (Esc) key to quit:
//       
//       a --- You pressed A 
//       k --- You pressed ALT+K 
//       ► --- You pressed CTL+P 
//         --- You pressed RightArrow 
//       R --- You pressed SHIFT+R 
//                --- You pressed CTL+I 
//       j --- You pressed ALT+J 
//       O --- You pressed SHIFT+O 
//       § --- You pressed CTL+U }
using System;

class Example 
{
   public static void Main() 
   {
      ConsoleKeyInfo cki;
      // Prevent example from ending if CTL+C is pressed.
      Console.TreatControlCAsInput = true;

      Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
      Console.WriteLine("Press the Escape (Esc) key to quit: \n");
      do 
      {
         cki = Console.ReadKey();
         Console.Write(" --- You pressed ");
         if((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
         if((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
         if((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
         Console.WriteLine(cki.Key.ToString());
       } while (cki.Key != ConsoleKey.Escape);
    }
}
// This example displays output similar to the following:
//       Press any combination of CTL, ALT, and SHIFT, and a console key.
//       Press the Escape (Esc) key to quit:
//       
//       a --- You pressed A 
//       k --- You pressed ALT+K 
//       ► --- You pressed CTL+P 
//         --- You pressed RightArrow 
//       R --- You pressed SHIFT+R 
//                --- You pressed CTL+I 
//       j --- You pressed ALT+J 
//       O --- You pressed SHIFT+O 
//       § --- You pressed CTL+U 
Class Example
   Public Shared Sub Main()
      Dim cki As ConsoleKeyInfo
      ' Prevent example from ending if CTL+C is pressed.
      Console.TreatControlCAsInput = True

      Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.")
      Console.WriteLine("Press the Escape (Esc) key to quit: " + vbCrLf)
      Do
         cki = Console.ReadKey()
         Console.Write(" --- You pressed ")
         If (cki.Modifiers And ConsoleModifiers.Alt) <> 0 Then Console.Write("ALT+")
         If (cki.Modifiers And ConsoleModifiers.Shift) <> 0 Then Console.Write("SHIFT+")
         If (cki.Modifiers And ConsoleModifiers.Control) <> 0 Then Console.Write("CTL+")
         Console.WriteLine(cki.Key.ToString)
      Loop While cki.Key <> ConsoleKey.Escape
   End Sub 
End Class 
' This example displays output similar to the following:
'       Press any combination of CTL, ALT, and SHIFT, and a console key.
'       Press the Escape (Esc) key to quit:
'       
'       a --- You pressed A 
'       k --- You pressed ALT+K 
'       ► --- You pressed CTL+P 
'         --- You pressed RightArrow 
'       R --- You pressed SHIFT+R 
'                --- You pressed CTL+I 
'       j --- You pressed ALT+J 
'       O --- You pressed SHIFT+O 
'       § --- You pressed CTL+U 

설명

합니다 ReadKey 메서드 기다리고, 즉, 스레드 실행 블록을 ReadKey 문자나 함수 키를 누를 때까지 메서드.The ReadKey method waits, that is, blocks on the thread issuing the ReadKey method, until a character or function key is pressed. 하나 이상의 Alt, Ctrl 또는 Shift 보조키를 사용 하 여 조합에서 문자 또는 함수 키를 누를 수입니다.A character or function key can be pressed in combination with one or more Alt, Ctrl, or Shift modifier keys. 그러나 자체적으로 보조 키를 누르면 발생 하지 것입니다는 ReadKey 메서드를 반환 합니다.However, pressing a modifier key by itself will not cause the ReadKey method to return.

애플리케이션에 따라 사용 하려는 합니다 ReadKey 와 함께에서 메서드는 KeyAvailable 속성입니다.Depending on your application, you might want to use the ReadKey method in conjunction with the KeyAvailable property.

합니다 ReadKey 표준 입력 파일을 리디렉션되는 경우에 키보드에서 읽고 메서드는 SetIn 메서드.The ReadKey method reads from the keyboard even if the standard input is redirected to a file with the SetIn method.

추가 정보

ReadKey(Boolean) ReadKey(Boolean) ReadKey(Boolean) ReadKey(Boolean)

다음 문자나 사용자가 누른 기능 키를 가져옵니다.Obtains the next character or function key pressed by the user. 누른 키는 선택적으로 콘솔 창에 표시됩니다.The pressed key is optionally displayed in the console window.

public:
 static ConsoleKeyInfo ReadKey(bool intercept);
public static ConsoleKeyInfo ReadKey (bool intercept);
static member ReadKey : bool -> ConsoleKeyInfo
Public Shared Function ReadKey (intercept As Boolean) As ConsoleKeyInfo

매개 변수

intercept
Boolean Boolean Boolean Boolean

누른 키를 콘솔 창에 표시할지 여부를 확인합니다.Determines whether to display the pressed key in the console window. 누른 키를 표시하지 않으려면 true이고 표시하려면 false입니다.true to not display the pressed key; otherwise, false.

반환

누른 콘솔 키에 해당하는 ConsoleKey 상수 및 유니코드 문자(있을 경우)를 설명하는 개체입니다.An object that describes the ConsoleKey constant and Unicode character, if any, that correspond to the pressed console key. ConsoleKeyInfo 개체는 하나 이상의 Shift, Alt 또는 Ctrl 보조키를 콘솔 키와 동시에 눌렀는지 여부를 나타내는 ConsoleModifiers 값을 비트 조합으로 나타냅니다.The ConsoleKeyInfo object also describes, in a bitwise combination of ConsoleModifiers values, whether one or more Shift, Alt, or Ctrl modifier keys was pressed simultaneously with the console key.

예외

In 속성이 콘솔 이외의 다른 스트림에서 리디렉션됩니다.The In property is redirected from some stream other than the console.

예제

가장 일반적인 이유 중 하나가 사용 하는 ReadKey 방법은 키를 누르면와 앱을 종료 하거나 정보는 추가 창이 표시 될 때까지 프로그램 실행을 중단 하는 것입니다.One of the most common uses of the ReadKey method is to halt program execution until the user presses a key and the app either terminates or displays an additional window of information. 다음 예제에서는 ReadKey(Boolean) 사용자를 앱을 종료 하기 전에 Enter 키를 누를 때까지 대기 하는 방법입니다.The following example uses the ReadKey(Boolean) method to wait for the user to press the Enter key before terminating the app. 사용자가 다른 키를 누르면이 표시 됩니다 콘솔로 note 합니다.Note that, if the user presses any other key, it is not echoed to the console.

using System;

public class Example
{
   public static void Main()
   {
      DateTime dat = DateTime.Now;
      Console.WriteLine("The time: {0:d} at {0:t}", dat);
      TimeZoneInfo tz = TimeZoneInfo.Local;
      Console.WriteLine("The time zone: {0}\n", 
                        tz.IsDaylightSavingTime(dat) ?
                           tz.DaylightName : tz.StandardName);
      Console.Write("Press <Enter> to exit... ");
      while (Console.ReadKey(true).Key != ConsoleKey.Enter) {}
   }
}
// The example displays output like the following:
//     The time: 11/11/2015 at 4:02 PM:
//     The time zone: Pacific Standard Time
Module Example
   Public Sub Main()
      Dim dat As Date = Date.Now
      Console.WriteLine("The time: {0:d} at {0:t}", dat)
      Dim tz As TimeZoneInfo = TimeZoneInfo.Local
      Console.WriteLine("The time zone: {0}", 
                        If(tz.IsDaylightSavingTime(dat),
                           tz.DaylightName, tz.StandardName))
      Console.WriteLine()
      Console.Write("Press <Enter> to exit... ")
      Do While Console.ReadKey(True).Key <> ConsoleKey.Enter
      Loop
   End Sub
End Module
' The example displays the following output:
'     The time: 11/11/2015 at 4:02 PM
'     The time zone: Pacific Standard Time

다음 예제에서는 ReadKey(Boolean) 콘솔에 해당 키를 에코 하지 않고 사용자가 누른 키에 대 한 정보를 표시 하는 방법입니다.The following example uses the ReadKey(Boolean) method to display information about the key pressed by a user without echoing that key to the console.

using namespace System;

void main()
{
   ConsoleKeyInfo cki;
   // Prevent example from ending if CTL+C is pressed.
   Console::TreatControlCAsInput = true;

   Console::WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
   Console::WriteLine("Press the Escape (Esc) key to quit: \n");
   do {
      cki = Console::ReadKey(true);
      Console::Write("You pressed ");
      if ((cki.Modifiers & ConsoleModifiers::Alt) != ConsoleModifiers()) Console::Write("ALT+");
      if ((cki.Modifiers & ConsoleModifiers::Shift) != ConsoleModifiers()) Console::Write("SHIFT+");
      if ((cki.Modifiers & ConsoleModifiers::Control) != ConsoleModifiers()) Console::Write("CTL+");
      Console::WriteLine("{0} (character '{1}')", cki.Key, cki.KeyChar);
   } while (cki.Key != ConsoleKey::Escape);
}
// This example displays output similar to the following:
//       Press any combination of CTL, ALT, and SHIFT, and a console key.
//       Press the Escape (Esc) key to quit:
//       
//       You pressed CTL+A (character '☺')
//       You pressed C (character 'c')
//       You pressed CTL+C (character '♥')
//       You pressed K (character 'k')
//       You pressed ALT+I (character 'i')
//       You pressed ALT+U (character 'u')
//       You pressed ALT+SHIFT+H (character 'H')
//       You pressed Escape (character '←')
using System;

class Example 
{
   public static void Main() 
   {
      ConsoleKeyInfo cki;
      // Prevent example from ending if CTL+C is pressed.
      Console.TreatControlCAsInput = true;

      Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
      Console.WriteLine("Press the Escape (Esc) key to quit: \n");
      do {
         cki = Console.ReadKey(true);
         Console.Write("You pressed ");
         if ((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
         if ((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
         if ((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
         Console.WriteLine("{0} (character '{1}')", cki.Key, cki.KeyChar);
      } while (cki.Key != ConsoleKey.Escape);
   }
}
// This example displays output similar to the following:
//       Press any combination of CTL, ALT, and SHIFT, and a console key.
//       Press the Escape (Esc) key to quit:
//       
//       You pressed CTL+A (character '☺')
//       You pressed C (character 'c')
//       You pressed CTL+C (character '♥')
//       You pressed K (character 'k')
//       You pressed ALT+I (character 'i')
//       You pressed ALT+U (character 'u')
//       You pressed ALT+SHIFT+H (character 'H')
//       You pressed Escape (character '←')
Class Example
   Public Shared Sub Main()
      Dim cki As ConsoleKeyInfo
      ' Prevent example from ending if CTL+C is pressed.
      Console.TreatControlCAsInput = True

      Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.")
      Console.WriteLine("Press the Escape (Esc) key to quit: " + vbCrLf)
      Do
         cki = Console.ReadKey(True)
         Console.Write("You pressed ")
         If (cki.Modifiers And ConsoleModifiers.Alt) <> 0 Then Console.Write("ALT+")
         If (cki.Modifiers And ConsoleModifiers.Shift) <> 0 Then Console.Write("SHIFT+")
         If (cki.Modifiers And ConsoleModifiers.Control) <> 0 Then Console.Write("CTL+")
         Console.WriteLine("{0} (character '{1}')", cki.Key, cki.KeyChar)
      Loop While cki.Key <> ConsoleKey.Escape
   End Sub
End Class 
' This example displays output similar to the following:
'       Press any combination of CTL, ALT, and SHIFT, and a console key.
'       Press the Escape (Esc) key to quit:
'       
'       You pressed CTL+A (character '☺')
'       You pressed C (character 'c')
'       You pressed CTL+C (character '♥')
'       You pressed K (character 'k')
'       You pressed ALT+I (character 'i')
'       You pressed ALT+U (character 'u')
'       You pressed ALT+SHIFT+H (character 'H')
'       You pressed Escape (character '←')

설명

합니다 ReadKey 메서드 기다리고, 즉, 스레드 실행 블록을 ReadKey 문자나 함수 키를 누를 때까지 메서드.The ReadKey method waits, that is, blocks on the thread issuing the ReadKey method, until a character or function key is pressed. 하나 이상의 Alt, Ctrl 또는 Shift 보조키를 사용 하 여 조합에서 문자 또는 함수 키를 누를 수입니다.A character or function key can be pressed in combination with one or more Alt, Ctrl, or Shift modifier keys. 그러나 자체적으로 보조 키를 누르면 발생 하지 것입니다는 ReadKey 메서드를 반환 합니다.However, pressing a modifier key by itself will not cause the ReadKey method to return.

경우는 intercept 매개 변수는 true, 누른된 키를 가로채 고 하지 콘솔 창에 표시 된이 고 그렇지 않으면, 누른된 키 표시 됩니다.If the intercept parameter is true, the pressed key is intercepted and not displayed in the console window; otherwise, the pressed key is displayed.

애플리케이션에 따라 사용 하려는 합니다 ReadKey 와 함께에서 메서드는 KeyAvailable 속성입니다.Depending on your application, you might want to use the ReadKey method in conjunction with the KeyAvailable property.

합니다 ReadKey 표준 입력 파일을 리디렉션되는 경우에 키보드에서 읽고 메서드는 SetIn 메서드.The ReadKey method reads from the keyboard even if the standard input is redirected to a file with the SetIn method.

추가 정보

적용 대상