ConsoleKeyInfo.KeyChar 속성

정의

현재 ConsoleKeyInfo 개체가 나타내는 유니코드 문자를 가져옵니다.Gets the Unicode character represented by the current ConsoleKeyInfo object.

public:
 property char KeyChar { char get(); };
public char KeyChar { get; }
member this.KeyChar : char
Public ReadOnly Property KeyChar As Char

속성 값

현재 ConsoleKeyInfo 개체가 나타내는 콘솔 키에 해당하는 개체입니다.An object that corresponds to the console key represented by the current ConsoleKeyInfo object.

예제

다음 예제에서는 KeyChar 속성을 사용 하 여 사용자가 입력 한 문자를 문자열에 추가 합니다.The following example uses the KeyChar property to add the characters input by the user into a string. 이 예에서는 ENTER, ESC백스페이스이외의 특수 키를 무시 합니다.The example ignores special keys other than ENTER, ESC, and BACKSPACE.

using System;

public class Example
{
   public static void Main()
   {
      // Configure console.
      Console.BufferWidth = 80;
      Console.WindowWidth = Console.BufferWidth;
      Console.TreatControlCAsInput = true;
      
      string inputString = String.Empty;
      ConsoleKeyInfo keyInfo;

      Console.WriteLine("Enter a string. Press <Enter> or Esc to exit.");
      do {
         keyInfo = Console.ReadKey(true);
         // Ignore if Alt or Ctrl is pressed.
         if ((keyInfo.Modifiers & ConsoleModifiers.Alt) == ConsoleModifiers.Alt) 
            continue;
         if ((keyInfo.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control)
            continue; 
         // Ignore if KeyChar value is \u0000.
         if (keyInfo.KeyChar == '\u0000') continue;
         // Ignore tab key.
         if (keyInfo.Key == ConsoleKey.Tab) continue;
         // Handle backspace.
         if (keyInfo.Key == ConsoleKey.Backspace) {
            // Are there any characters to erase?
            if (inputString.Length >= 1) { 
               // Determine where we are in the console buffer.
               int cursorCol = Console.CursorLeft - 1;
               int oldLength = inputString.Length;
               int extraRows = oldLength / 80;

               inputString = inputString.Substring(0, oldLength - 1);
               Console.CursorLeft = 0;
               Console.CursorTop = Console.CursorTop - extraRows;
               Console.Write(inputString + new String(' ', oldLength - inputString.Length));
               Console.CursorLeft = cursorCol;
            }
            continue;
         }
         // Handle Escape key.
         if (keyInfo.Key == ConsoleKey.Escape) break;
         // Handle key by adding it to input string.
         Console.Write(keyInfo.KeyChar);
         inputString += keyInfo.KeyChar;
      } while (keyInfo.Key != ConsoleKey.Enter);
      Console.WriteLine("\n\nYou entered:\n    {0}", 
                        String.IsNullOrEmpty(inputString) ? "<nothing>" : inputString);
   }
}
Module Example
   Public Sub Main()
      ' Configure console.
      Console.BufferWidth = 80
      Console.WindowWidth = Console.BufferWidth
      Console.TreatControlCAsInput = True
      
      Dim inputString As String = String.Empty
      Dim keyInfo As ConsoleKeyInfo

      Console.WriteLine("Enter a string. Press <Enter> or Esc to exit.")
      Do
         keyInfo = Console.ReadKey(True)
         ' Ignore if Alt or Ctrl is pressed.
         If (keyInfo.Modifiers And ConsoleModifiers.Alt) = ConsoleModifiers.Alt _
                              Then Continue Do 
         If (keyInfo.Modifiers And ConsoleModifiers.Control) = ConsoleModifiers.Control _
                              Then Continue Do 
         ' Ignore if KeyChar value is \u0000.
         If keyInfo.KeyChar = ChrW(0) Then Continue Do
         ' Ignore tab, clear key.
         If keyInfo.Key = ConsoleKey.Tab Then Continue Do
         ' Handle backspace.
         If keyInfo.Key = ConsoleKey.Backspace Then
            ' Are there any characters to erase?
            If inputString.Length >= 1 Then
               ' Determine where we are in the console buffer.
               Dim cursorCol As Integer = Console.CursorLeft - 1
               Dim oldLength As Integer = inputString.Length
               Dim extraRows As Integer = oldLength \ 80

               inputString = inputString.Substring(0, oldLength - 1)
               Console.CursorLeft = 0
               Console.CursorTop = Console.CursorTop - extraRows
               Console.Write(inputString + New String(" "c, oldLength - inputString.Length))
               Console.CursorLeft = cursorCol
            End If
            Continue Do
         End If   
         ' Handle Escape key.
         If keyInfo.Key = ConsoleKey.Escape Then Exit Do
         ' Handle key by adding it to input string.
         Console.Write(keyInfo.KeyChar)
         inputString += keyInfo.KeyChar 
      Loop While keyInfo.Key <> ConsoleKey.Enter
      Console.WriteLine("{0}{0}You entered:{0}    {1}", vbCrLf, _
                        IIf(String.IsNullOrEmpty(inputString), "<nothing>", inputString))
   End Sub
End Module

설명

누른 키가 유니코드 문자에 매핑되지 않는 경우 (예: 사용자가 F1 키 또는 Home 키를 누르는 경우) KeyChar 속성의 값은 \U0000.입니다.If the key pressed does not map to a Unicode character (for example, if the user presses the F1 key or the Home key), the value of the KeyChar property is \U0000.

적용 대상