Export (0) Print
Expand All
Expand Minimize

ConsoleKey Enumeration

Specifies the standard keys on a console.

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

[SerializableAttribute]
public enum ConsoleKey

Member nameDescription
BackspaceThe BACKSPACE key.
TabThe TAB key.
ClearThe CLEAR key.
EnterThe ENTER key.
PauseThe PAUSE key.
EscapeThe ESC (ESCAPE) key.
SpacebarThe SPACEBAR key.
PageUpThe PAGE UP key.
PageDownThe PAGE DOWN key.
EndThe END key.
HomeThe HOME key.
LeftArrowThe LEFT ARROW key.
UpArrowThe UP ARROW key.
RightArrowThe RIGHT ARROW key.
DownArrowThe DOWN ARROW key.
SelectThe SELECT key.
PrintThe PRINT key.
ExecuteThe EXECUTE key.
PrintScreenThe PRINT SCREEN key.
InsertThe INS (INSERT) key.
DeleteThe DEL (DELETE) key.
HelpThe HELP key.
D0The 0 key.
D1The 1 key.
D2The 2 key.
D3The 3 key.
D4The 4 key.
D5The 5 key.
D6The 6 key.
D7The 7 key.
D8The 8 key.
D9The 9 key.
AThe A key.
BThe B key.
CThe C key.
DThe D key.
EThe E key.
FThe F key.
GThe G key.
HThe H key.
IThe I key.
JThe J key.
KThe K key.
LThe L key.
MThe M key.
NThe N key.
OThe O key.
PThe P key.
QThe Q key.
RThe R key.
SThe S key.
TThe T key.
UThe U key.
VThe V key.
WThe W key.
XThe X key.
YThe Y key.
ZThe Z key.
LeftWindowsThe left Windows logo key (Microsoft Natural Keyboard).
RightWindowsThe right Windows logo key (Microsoft Natural Keyboard).
ApplicationsThe Application key (Microsoft Natural Keyboard).
SleepThe Computer Sleep key.
NumPad0The 0 key on the numeric keypad.
NumPad1The 1 key on the numeric keypad.
NumPad2The 2 key on the numeric keypad.
NumPad3The 3 key on the numeric keypad.
NumPad4The 4 key on the numeric keypad.
NumPad5The 5 key on the numeric keypad.
NumPad6The 6 key on the numeric keypad.
NumPad7The 7 key on the numeric keypad.
NumPad8The 8 key on the numeric keypad.
NumPad9The 9 key on the numeric keypad.
MultiplyThe Multiply key.
AddThe Add key.
SeparatorThe Separator key.
SubtractThe Subtract key.
DecimalThe Decimal key.
DivideThe Divide key.
F1The F1 key.
F2The F2 key.
F3The F3 key.
F4The F4 key.
F5The F5 key.
F6The F6 key.
F7The F7 key.
F8The F8 key.
F9The F9 key.
F10The F10 key.
F11The F11 key.
F12The F12 key.
F13The F13 key.
F14The F14 key.
F15The F15 key.
F16The F16 key.
F17The F17 key.
F18The F18 key.
F19The F19 key.
F20The F20 key.
F21The F21 key.
F22The F22 key.
F23The F23 key.
F24The F24 key.
BrowserBackThe Browser Back key (Windows 2000 or later).
BrowserForwardThe Browser Forward key (Windows 2000 or later).
BrowserRefreshThe Browser Refresh key (Windows 2000 or later).
BrowserStopThe Browser Stop key (Windows 2000 or later).
BrowserSearchThe Browser Search key (Windows 2000 or later).
BrowserFavoritesThe Browser Favorites key (Windows 2000 or later).
BrowserHomeThe Browser Home key (Windows 2000 or later).
VolumeMuteThe Volume Mute key (Microsoft Natural Keyboard, Windows 2000 or later).
VolumeDownThe Volume Down key (Microsoft Natural Keyboard, Windows 2000 or later).
VolumeUpThe Volume Up key (Microsoft Natural Keyboard, Windows 2000 or later).
MediaNextThe Media Next Track key (Windows 2000 or later).
MediaPreviousThe Media Previous Track key (Windows 2000 or later).
MediaStopThe Media Stop key (Windows 2000 or later).
MediaPlayThe Media Play/Pause key (Windows 2000 or later).
LaunchMailThe Start Mail key (Microsoft Natural Keyboard, Windows 2000 or later).
LaunchMediaSelectThe Select Media key (Microsoft Natural Keyboard, Windows 2000 or later).
LaunchApp1The Start Application 1 key (Microsoft Natural Keyboard, Windows 2000 or later).
LaunchApp2The Start Application 2 key (Microsoft Natural Keyboard, Windows 2000 or later).
Oem1The OEM 1 key (OEM specific).
OemPlusThe OEM Plus key on any country/region keyboard (Windows 2000 or later).
OemCommaThe OEM Comma key on any country/region keyboard (Windows 2000 or later).
OemMinusThe OEM Minus key on any country/region keyboard (Windows 2000 or later).
OemPeriodThe OEM Period key on any country/region keyboard (Windows 2000 or later).
Oem2The OEM 2 key (OEM specific).
Oem3The OEM 3 key (OEM specific).
Oem4The OEM 4 key (OEM specific).
Oem5The OEM 5 (OEM specific).
Oem6The OEM 6 key (OEM specific).
Oem7The OEM 7 key (OEM specific).
Oem8The OEM 8 key (OEM specific).
Oem102The OEM 102 key (OEM specific).
ProcessThe IME PROCESS key.
PacketThe PACKET key (used to pass Unicode characters with keystrokes).
AttentionThe ATTN key.
CrSelThe CRSEL (CURSOR SELECT) key.
ExSelThe EXSEL (EXTEND SELECTION) key.
EraseEndOfFileThe ERASE EOF key.
PlayThe PLAY key.
ZoomThe ZOOM key.
NoNameA constant reserved for future use.
Pa1The PA1 key.
OemClearThe 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.

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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, 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

Community Additions

ADD
Show:
© 2014 Microsoft