Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
 ConsoleKey Enumeration
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ConsoleKey Enumeration

Updated: November 2007

Specifies the standard keys on a console.

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

Visual Basic (Declaration)
<SerializableAttribute> _
Public Enumeration ConsoleKey
Visual Basic (Usage)
Dim instance As ConsoleKey
C#
[SerializableAttribute]
public enum ConsoleKey
Visual C++
[SerializableAttribute]
public enum class ConsoleKey
J#
/** @attribute SerializableAttribute */ 
public enum ConsoleKey
JScript
public enum ConsoleKey
Member nameDescription
Backspace The BACKSPACE key.
Tab The TAB key.
Clear The CLEAR key.
Enter The ENTER key.
Pause The PAUSE key.
Escape The ESC (ESCAPE) key.
Spacebar The SPACEBAR key.
PageUp The PAGE UP key.
PageDown The PAGE DOWN key.
End The END key.
Home The HOME key.
LeftArrow The LEFT ARROW key.
UpArrow The UP ARROW key.
RightArrow The RIGHT ARROW key.
DownArrow The DOWN ARROW key.
Select The SELECT key.
Print The PRINT key.
Execute The EXECUTE key.
PrintScreen The PRINT SCREEN key.
Insert The INS (INSERT) key.
Delete The DEL (DELETE) key.
Help The HELP key.
D0 The 0 key.
D1 The 1 key.
D2 The 2 key.
D3 The 3 key.
D4 The 4 key.
D5 The 5 key.
D6 The 6 key.
D7 The 7 key.
D8 The 8 key.
D9 The 9 key.
A The A key.
B The B key.
C The C key.
D The D key.
E The E key.
F The F key.
G The G key.
H The H key.
I The I key.
J The J key.
K The K key.
L The L key.
M The M key.
N The N key.
O The O key.
P The P key.
Q The Q key.
R The R key.
S The S key.
T The T key.
U The U key.
V The V key.
W The W key.
X The X key.
Y The Y key.
Z The Z key.
LeftWindows The left Windows logo key (Microsoft Natural Keyboard).
RightWindows The right Windows logo key (Microsoft Natural Keyboard).
Applications The Application key (Microsoft Natural Keyboard).
Sleep The Computer Sleep key.
NumPad0 The 0 key on the numeric keypad.
NumPad1 The 1 key on the numeric keypad.
NumPad2 The 2 key on the numeric keypad.
NumPad3 The 3 key on the numeric keypad.
NumPad4 The 4 key on the numeric keypad.
NumPad5 The 5 key on the numeric keypad.
NumPad6 The 6 key on the numeric keypad.
NumPad7 The 7 key on the numeric keypad.
NumPad8 The 8 key on the numeric keypad.
NumPad9 The 9 key on the numeric keypad.
Multiply The Multiply key.
Add The Add key.
Separator The Separator key.
Subtract The Subtract key.
Decimal The Decimal key.
Divide The Divide key.
F1 The F1 key.
F2 The F2 key.
F3 The F3 key.
F4 The F4 key.
F5 The F5 key.
F6 The F6 key.
F7 The F7 key.
F8 The F8 key.
F9 The F9 key.
F10 The F10 key.
F11 The F11 key.
F12 The F12 key.
F13 The F13 key.
F14 The F14 key.
F15 The F15 key.
F16 The F16 key.
F17 The F17 key.
F18 The F18 key.
F19 The F19 key.
F20 The F20 key.
F21 The F21 key.
F22 The F22 key.
F23 The F23 key.
F24 The F24 key.
BrowserBack The Browser Back key (Windows 2000 or later).
BrowserForward The Browser Forward key (Windows 2000 or later).
BrowserRefresh The Browser Refresh key (Windows 2000 or later).
BrowserStop The Browser Stop key (Windows 2000 or later).
BrowserSearch The Browser Search key (Windows 2000 or later).
BrowserFavorites The Browser Favorites key (Windows 2000 or later).
BrowserHome The Browser Home key (Windows 2000 or later).
VolumeMute The Volume Mute key (Microsoft Natural Keyboard, Windows 2000 or later).
VolumeDown The Volume Down key (Microsoft Natural Keyboard, Windows 2000 or later).
VolumeUp The Volume Up key (Microsoft Natural Keyboard, Windows 2000 or later).
MediaNext The Media Next Track key (Windows 2000 or later).
MediaPrevious The Media Previous Track key (Windows 2000 or later).
MediaStop The Media Stop key (Windows 2000 or later).
MediaPlay The Media Play/Pause key (Windows 2000 or later).
LaunchMail The Start Mail key (Microsoft Natural Keyboard, Windows 2000 or later).
LaunchMediaSelect The Select Media key (Microsoft Natural Keyboard, Windows 2000 or later).
LaunchApp1 The Start Application 1 key (Microsoft Natural Keyboard, Windows 2000 or later).
LaunchApp2 The Start Application 2 key (Microsoft Natural Keyboard, Windows 2000 or later).
Oem1 The OEM 1 key (OEM specific).
OemPlus The OEM Plus key on any country/region keyboard (Windows 2000 or later).
OemComma The OEM Comma key on any country/region keyboard (Windows 2000 or later).
OemMinus The OEM Minus key on any country/region keyboard (Windows 2000 or later).
OemPeriod The OEM Period key on any country/region keyboard (Windows 2000 or later).
Oem2 The OEM 2 key (OEM specific).
Oem3 The OEM 3 key (OEM specific).
Oem4 The OEM 4 key (OEM specific).
Oem5 The OEM 5 (OEM specific).
Oem6 The OEM 6 key (OEM specific).
Oem7 The OEM 7 key (OEM specific).
Oem8 The OEM 8 key (OEM specific).
Oem102 The OEM 102 key (OEM specific).
Process The IME PROCESS key.
Packet The PACKET key (used to pass Unicode characters with keystrokes).
Attention The ATTN key.
CrSel The CRSEL (CURSOR SELECT) key.
ExSel The EXSEL (EXTEND SELECTION) key.
EraseEndOfFile The ERASE EOF key.
Play The PLAY key.
Zoom The ZOOM key.
NoName A constant reserved for future use.
Pa1 The PA1 key.
OemClear The CLEAR key (OEM specific).

The ConsoleKey enumeration is typically used in the System..::.ConsoleKeyInfo structure, which is returned by the Console..::.ReadKey method to indicate which key on the console has been pressed.

The following example uses the ConsoleKey enumeration to indicate to the user which key the user had pressed.

Visual Basic
Imports System.Text

Module ConsoleKeyExample

   Public Sub Main()
      Dim input As ConsoleKeyInfo
      Do 
         Console.WriteLine("Press a key, together with Alt, Ctrl, or Shift.")
         Console.WriteLine("Press Esc to exit.")
         input = Console.ReadKey(True)

         Dim output As New StringBuilder(String.Format("You pressed {0}", input.Key.ToString()))
         Dim modifiers As Boolean

         If (input.Modifiers And ConsoleModifiers.Alt) = ConsoleModifiers.Alt Then
            output.Append(", together with " + ConsoleModifiers.Alt.ToString())
            modifiers = True
         End If
         If (input.Modifiers And ConsoleModifiers.Control) = ConsoleModifiers.Control Then
            If modifiers Then
               output.Append(" and ")
            Else
               output.Append(", together with ")
               modifiers = True
            End If
            output.Append(ConsoleModifiers.Control.ToString)
         End If
         If (input.Modifiers And ConsoleModifiers.Shift) = ConsoleModifiers.Shift Then
            If modifiers Then
               output.Append(" and ")
            Else
               output.Append(", together with ")
            End If
            output.Append(ConsoleModifiers.Shift.ToString)
         End If
         output.Append(".")                  
         Console.WriteLine(output.ToString())
         Console.WriteLine()
      Loop While input.Key <> ConsoleKey.Escape        
   End Sub
End Module
' The output from a sample console session might appear as follows:
'       Press a key, along with Alt, Ctrl, or Shift.
'       Press Esc to exit.
'       You pressed D.
'       
'       Press a key, along with Alt, Ctrl, or Shift.
'       Press Esc to exit.
'       You pressed X, along with Shift.
'       
'       Press a key, along with Alt, Ctrl, or Shift.
'       Press Esc to exit.
'       You pressed L, along with Control and Shift.
'       
'       Press a key, along with Alt, Ctrl, or Shift.
'       Press Esc to exit.
'       You pressed P, along with Alt and Control and Shift.
'       
'       Press a key, along with Alt, Ctrl, or Shift.
'       Press Esc to exit.
'       You pressed Escape. 

C#
using System;
using System.Text;

public class ConsoleKeyExample
{
   public static void Main()
   {
      ConsoleKeyInfo input;
      do {
         Console.WriteLine("Press a key, together with Alt, Ctrl, or Shift.");
         Console.WriteLine("Press Esc to exit.");
         input = Console.ReadKey(true);

         StringBuilder output = new StringBuilder(
                       String.Format("You pressed {0}", input.Key.ToString()));
         bool modifiers = false;

         if ((input.Modifiers & ConsoleModifiers.Alt) == ConsoleModifiers.Alt) {
            output.Append(", together with " + ConsoleModifiers.Alt.ToString());
            modifiers = true;
         }
         if ((input.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control)
         {
            if (modifiers) {
               output.Append(" and ");
            }   
            else {
               output.Append(", together with ");
               modifiers = true;
            }
            output.Append(ConsoleModifiers.Control.ToString());
         }
         if ((input.Modifiers & ConsoleModifiers.Shift) == ConsoleModifiers.Shift)
         {
            if (modifiers) {
               output.Append(" and ");
            }   
            else {
               output.Append(", together with ");
               modifiers = true;
            }
            output.Append(ConsoleModifiers.Shift.ToString());
         }
         output.Append(".");                  
         Console.WriteLine(output.ToString());
         Console.WriteLine();
      } while (input.Key != ConsoleKey.Escape);
   }
}
// The output from a sample console session might appear as follows:
//       Press a key, along with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed D.
//       
//       Press a key, along with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed X, along with Shift.
//       
//       Press a key, along with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed L, along with Control and Shift.
//       
//       Press a key, along with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed P, along with Alt and Control and Shift.
//       
//       Press a key, along with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed Escape. 

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Reference

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker