ConsoleKeyInfo 구조체

정의

콘솔 키로 표현된 문자와 Shift, Alt 및 Ctrl 보조키의 상태를 포함하여 누른 콘솔 키를 설명합니다.

public value class ConsoleKeyInfo : IEquatable<ConsoleKeyInfo>
public value class ConsoleKeyInfo
public readonly struct ConsoleKeyInfo : IEquatable<ConsoleKeyInfo>
public struct ConsoleKeyInfo
public readonly struct ConsoleKeyInfo
[System.Serializable]
public struct ConsoleKeyInfo
type ConsoleKeyInfo = struct
[<System.Serializable>]
type ConsoleKeyInfo = struct
Public Structure ConsoleKeyInfo
Implements IEquatable(Of ConsoleKeyInfo)
Public Structure ConsoleKeyInfo
상속
ConsoleKeyInfo
특성
구현

예제

다음 예제에서는 읽기 작업에서 개체를 ConsoleKeyInfo 사용하는 방법을 보여 줍니다.

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
open System

// Prevent example from ending if CTL+C is pressed.
Console.TreatControlCAsInput <- true

printfn "Press any combination of CTL, ALT, and SHIFT, and a console key."
printfn "Press the Escape (Esc) key to quit: \n"

let mutable cki = Unchecked.defaultof<ConsoleKeyInfo>

while cki.Key <> ConsoleKey.Escape do
    cki <- Console.ReadKey()
    printf " --- You pressed "
    if int (cki.Modifiers &&& ConsoleModifiers.Alt) <> 0 then printf "ALT+"
    if int (cki.Modifiers &&& ConsoleModifiers.Shift) <> 0 then printf "SHIFT+"
    if int (cki.Modifiers &&& ConsoleModifiers.Control) <> 0 then printf "CTL+"
    printfn $"{cki.Key}"


// 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

설명

ConsoleKeyInfo 형식은 사용자가 만들 수 없습니다. 대신 메서드 호출에 대한 응답으로 사용자에게 반환됩니다 Console.ReadKey .

개체는 ConsoleKeyInfo 누른 콘솔 키에 ConsoleKey 해당하는 상수 및 유니코드 문자(있는 경우)를 설명합니다. 또한 개체는 ConsoleKeyInfo 하나 이상의 Shift, Alt 또는 Ctrl 한정자 키를 콘솔 키와 동시에 눌렀는지 여부에 관계없이 값의 ConsoleModifiers 비트 조합으로 설명합니다.

생성자

ConsoleKeyInfo(Char, ConsoleKey, Boolean, Boolean, Boolean)

지정된 문자, 콘솔 키 및 보조키를 사용하여 ConsoleKeyInfo 구조체의 새 인스턴스를 초기화합니다.

속성

Key

현재 ConsoleKeyInfo 개체가 나타내는 콘솔 키를 가져옵니다.

KeyChar

현재 ConsoleKeyInfo 개체가 나타내는 유니코드 문자를 가져옵니다.

Modifiers

하나 이상의 보조키를 콘솔 키와 동시에 눌렀음을 지정하는 ConsoleModifiers 값의 비트 조합을 가져옵니다.

메서드

Equals(ConsoleKeyInfo)

지정한 ConsoleKeyInfo 개체가 현재 ConsoleKeyInfo 개체와 같은지 여부를 나타내는 값을 가져옵니다.

Equals(Object)

지정한 개체가 현재 ConsoleKeyInfo 개체와 같은지 여부를 나타내는 값을 가져옵니다.

GetHashCode()

현재 ConsoleKeyInfo 개체에 대한 해시 코드를 반환합니다.

연산자

Equality(ConsoleKeyInfo, ConsoleKeyInfo)

지정된 ConsoleKeyInfo 개체가 같은지 여부를 나타냅니다.

Inequality(ConsoleKeyInfo, ConsoleKeyInfo)

지정된 ConsoleKeyInfo 개체가 같은지 여부를 나타냅니다.

적용 대상

추가 정보