ConsoleKeyInfo.KeyChar Property

Gets the Unicode character represented by the current ConsoleKeyInfo object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public char KeyChar { get; }

Property Value

Type: System.Char
An object that corresponds to the console key represented by the current ConsoleKeyInfo object.

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.

The following example uses the KeyChar property to add the characters input by the user into a string. 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 cursorRow = Console.CursorTop;
               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);
   }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft