共用方式為


ConsoleKeyInfo.KeyChar 屬性

定義

取得目前的 ConsoleKeyInfo 物件表示的 Unicode 字元。

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

屬性值

Char

物件,該物件對應於目前的 ConsoleKeyInfo 物件所表示的主控台按鍵。

範例

下列範例會 KeyChar 使用 屬性,將使用者輸入的字元加入字串中。 此範例會忽略 ENTERESCBACKSPACE 以外的特殊索引鍵。

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

// Configure console.
Console.TreatControlCAsInput <- true

printfn "Enter a string. Press <Enter> or Esc to exit."

let mutable inputString = String.Empty
let mutable keyInfo = Unchecked.defaultof<ConsoleKeyInfo>

while keyInfo.Key <> ConsoleKey.Enter && keyInfo.Key <> ConsoleKey.Escape do
    keyInfo <- Console.ReadKey true
    
    // Ignore if Alt or Ctrl is pressed.
    if keyInfo.Modifiers &&& ConsoleModifiers.Alt <> ConsoleModifiers.Alt &&
       keyInfo.Modifiers &&& ConsoleModifiers.Control <> ConsoleModifiers.Control &&
       // Ignore if KeyChar value is \u0000.
       keyInfo.KeyChar <> '\u0000' &&
       // Ignore tab key.
       keyInfo.Key <> ConsoleKey.Tab then
        
        // 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.
                let cursorCol = Console.CursorLeft - 1
                let oldLength = inputString.Length
                let extraRows = oldLength / 80

                inputString <- inputString.Substring(0, oldLength - 1)
                Console.CursorLeft <- 0
                Console.CursorTop <- Console.CursorTop - extraRows
                printf $"{inputString + String(' ', oldLength - inputString.Length)}"
                Console.CursorLeft <- cursorCol
        else
            // Handle key by adding it to input string.
            printf $"{keyInfo.KeyChar}"
            inputString <- inputString + string keyInfo.KeyChar
    
printfn $"""

You entered:
    {if String.IsNullOrEmpty inputString then "<nothing>" else 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

備註

例如,如果按下的按鍵不會對應至 Unicode 字元 (,如果使用者按下 F1 鍵或 Home 鍵) , KeyChar 則 屬性值為 \U0000。

適用於