Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 Keys Enumeration

  Switch on low bandwidth view
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
Keys Enumeration

Specifies key codes and modifiers.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
<FlagsAttribute> _
<TypeConverterAttribute(GetType(KeysConverter))> _
Public Enumeration Keys
Visual Basic (Usage)
Dim instance As Keys
C#
[ComVisibleAttribute(true)]
[FlagsAttribute]
[TypeConverterAttribute(typeof(KeysConverter))]
public enum Keys
Visual C++
[ComVisibleAttribute(true)]
[FlagsAttribute]
[TypeConverterAttribute(typeof(KeysConverter))]
public enum class Keys
JScript
public enum Keys
Member nameDescription
Supported by the .NET Compact FrameworkKeyCode The bitmask to extract a key code from a key value.
Supported by the .NET Compact FrameworkModifiers The bitmask to extract modifiers from a key value.
Supported by the .NET Compact FrameworkNone No key pressed.
Supported by the .NET Compact FrameworkLButton The left mouse button.
Supported by the .NET Compact FrameworkRButton The right mouse button.
Supported by the .NET Compact FrameworkCancel The CANCEL key.
Supported by the .NET Compact FrameworkMButton The middle mouse button (three-button mouse).
Supported by the .NET Compact FrameworkXButton1 The first x mouse button (five-button mouse).
Supported by the .NET Compact FrameworkXButton2 The second x mouse button (five-button mouse).
Supported by the .NET Compact FrameworkBack The BACKSPACE key.
Supported by the .NET Compact FrameworkTab The TAB key.
Supported by the .NET Compact FrameworkLineFeed The LINEFEED key.
Supported by the .NET Compact FrameworkClear The CLEAR key.
Supported by the .NET Compact FrameworkReturn The RETURN key.
Supported by the .NET Compact FrameworkEnter The ENTER key.
Supported by the .NET Compact FrameworkShiftKey The SHIFT key.
Supported by the .NET Compact FrameworkControlKey The CTRL key.
Supported by the .NET Compact FrameworkMenu The ALT key.
Supported by the .NET Compact FrameworkPause The PAUSE key.
Supported by the .NET Compact FrameworkCapital The CAPS LOCK key.
Supported by the .NET Compact FrameworkCapsLock The CAPS LOCK key.
KanaMode The IME Kana mode key.
HanguelMode The IME Hanguel mode key. (maintained for compatibility; use HangulMode)
HangulMode The IME Hangul mode key.
JunjaMode The IME Junja mode key.
FinalMode The IME final mode key.
HanjaMode The IME Hanja mode key.
KanjiMode The IME Kanji mode key.
Supported by the .NET Compact FrameworkEscape The ESC key.
IMEConvert The IME convert key.
IMENonconvert The IME nonconvert key.
IMEAccept The IME accept key, replaces IMEAceept.
IMEAceept The IME accept key. Obsolete, use IMEAccept instead.
IMEModeChange The IME mode change key.
Supported by the .NET Compact FrameworkSpace The SPACEBAR key.
Supported by the .NET Compact FrameworkPrior The PAGE UP key.
Supported by the .NET Compact FrameworkPageUp The PAGE UP key.
Supported by the .NET Compact FrameworkNext The PAGE DOWN key.
Supported by the .NET Compact FrameworkPageDown The PAGE DOWN key.
Supported by the .NET Compact FrameworkEnd The END key.
Supported by the .NET Compact FrameworkHome The HOME key.
Supported by the .NET Compact FrameworkLeft The LEFT ARROW key.
Supported by the .NET Compact FrameworkUp The UP ARROW key.
Supported by the .NET Compact FrameworkRight The RIGHT ARROW key.
Supported by the .NET Compact FrameworkDown The DOWN ARROW key.
Supported by the .NET Compact FrameworkSelect The SELECT key.
Supported by the .NET Compact FrameworkPrint The PRINT key.
Supported by the .NET Compact FrameworkExecute The EXECUTE key.
Supported by the .NET Compact FrameworkSnapshot The PRINT SCREEN key.
Supported by the .NET Compact FrameworkPrintScreen The PRINT SCREEN key.
Supported by the .NET Compact FrameworkInsert The INS key.
Supported by the .NET Compact FrameworkDelete The DEL key.
Supported by the .NET Compact FrameworkHelp The HELP key.
Supported by the .NET Compact FrameworkD0 The 0 key.
Supported by the .NET Compact FrameworkD1 The 1 key.
Supported by the .NET Compact FrameworkD2 The 2 key.
Supported by the .NET Compact FrameworkD3 The 3 key.
Supported by the .NET Compact FrameworkD4 The 4 key.
Supported by the .NET Compact FrameworkD5 The 5 key.
Supported by the .NET Compact FrameworkD6 The 6 key.
Supported by the .NET Compact FrameworkD7 The 7 key.
Supported by the .NET Compact FrameworkD8 The 8 key.
Supported by the .NET Compact FrameworkD9 The 9 key.
Supported by the .NET Compact FrameworkA The A key.
Supported by the .NET Compact FrameworkB The B key.
Supported by the .NET Compact FrameworkC The C key.
Supported by the .NET Compact FrameworkD The D key.
Supported by the .NET Compact FrameworkE The E key.
Supported by the .NET Compact FrameworkF The F key.
Supported by the .NET Compact FrameworkG The G key.
Supported by the .NET Compact FrameworkH The H key.
Supported by the .NET Compact FrameworkI The I key.
Supported by the .NET Compact FrameworkJ The J key.
Supported by the .NET Compact FrameworkK The K key.
Supported by the .NET Compact FrameworkL The L key.
Supported by the .NET Compact FrameworkM The M key.
Supported by the .NET Compact FrameworkN The N key.
Supported by the .NET Compact FrameworkO The O key.
Supported by the .NET Compact FrameworkP The P key.
Supported by the .NET Compact FrameworkQ The Q key.
Supported by the .NET Compact FrameworkR The R key.
Supported by the .NET Compact FrameworkS The S key.
Supported by the .NET Compact FrameworkT The T key.
Supported by the .NET Compact FrameworkU The U key.
Supported by the .NET Compact FrameworkV The V key.
Supported by the .NET Compact FrameworkW The W key.
Supported by the .NET Compact FrameworkX The X key.
Supported by the .NET Compact FrameworkY The Y key.
Supported by the .NET Compact FrameworkZ The Z key.
Supported by the .NET Compact FrameworkLWin The left Windows logo key (Microsoft Natural Keyboard).
Supported by the .NET Compact FrameworkRWin The right Windows logo key (Microsoft Natural Keyboard).
Supported by the .NET Compact FrameworkApps The application key (Microsoft Natural Keyboard).
Sleep The computer sleep key.
Supported by the .NET Compact FrameworkNumPad0 The 0 key on the numeric keypad.
Supported by the .NET Compact FrameworkNumPad1 The 1 key on the numeric keypad.
Supported by the .NET Compact FrameworkNumPad2 The 2 key on the numeric keypad.
Supported by the .NET Compact FrameworkNumPad3 The 3 key on the numeric keypad.
Supported by the .NET Compact FrameworkNumPad4 The 4 key on the numeric keypad.
Supported by the .NET Compact FrameworkNumPad5 The 5 key on the numeric keypad.
Supported by the .NET Compact FrameworkNumPad6 The 6 key on the numeric keypad.
Supported by the .NET Compact FrameworkNumPad7 The 7 key on the numeric keypad.
Supported by the .NET Compact FrameworkNumPad8 The 8 key on the numeric keypad.
Supported by the .NET Compact FrameworkNumPad9 The 9 key on the numeric keypad.
Supported by the .NET Compact FrameworkMultiply The multiply key.
Supported by the .NET Compact FrameworkAdd The add key.
Supported by the .NET Compact FrameworkSeparator The separator key.
Supported by the .NET Compact FrameworkSubtract The subtract key.
Supported by the .NET Compact FrameworkDecimal The decimal key.
Supported by the .NET Compact FrameworkDivide The divide key.
Supported by the .NET Compact FrameworkF1 The F1 key.
Supported by the .NET Compact FrameworkF2 The F2 key.
Supported by the .NET Compact FrameworkF3 The F3 key.
Supported by the .NET Compact FrameworkF4 The F4 key.
Supported by the .NET Compact FrameworkF5 The F5 key.
Supported by the .NET Compact FrameworkF6 The F6 key.
Supported by the .NET Compact FrameworkF7 The F7 key.
Supported by the .NET Compact FrameworkF8 The F8 key.
Supported by the .NET Compact FrameworkF9 The F9 key.
Supported by the .NET Compact FrameworkF10 The F10 key.
Supported by the .NET Compact FrameworkF11 The F11 key.
Supported by the .NET Compact FrameworkF12 The F12 key.
Supported by the .NET Compact FrameworkF13 The F13 key.
Supported by the .NET Compact FrameworkF14 The F14 key.
Supported by the .NET Compact FrameworkF15 The F15 key.
Supported by the .NET Compact FrameworkF16 The F16 key.
Supported by the .NET Compact FrameworkF17 The F17 key.
Supported by the .NET Compact FrameworkF18 The F18 key.
Supported by the .NET Compact FrameworkF19 The F19 key.
Supported by the .NET Compact FrameworkF20 The F20 key.
Supported by the .NET Compact FrameworkF21 The F21 key.
Supported by the .NET Compact FrameworkF22 The F22 key.
Supported by the .NET Compact FrameworkF23 The F23 key.
Supported by the .NET Compact FrameworkF24 The F24 key.
Supported by the .NET Compact FrameworkNumLock The NUM LOCK key.
Supported by the .NET Compact FrameworkScroll The SCROLL LOCK key.
Supported by the .NET Compact FrameworkLShiftKey The left SHIFT key.
Supported by the .NET Compact FrameworkRShiftKey The right SHIFT key.
Supported by the .NET Compact FrameworkLControlKey The left CTRL key.
Supported by the .NET Compact FrameworkRControlKey The right CTRL key.
Supported by the .NET Compact FrameworkLMenu The left ALT key.
Supported by the .NET Compact FrameworkRMenu The right ALT 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 (Windows 2000 or later).
VolumeDown The volume down key (Windows 2000 or later).
VolumeUp The volume up key (Windows 2000 or later).
MediaNextTrack The media next track key (Windows 2000 or later).
MediaPreviousTrack The media previous track key (Windows 2000 or later).
MediaStop The media Stop key (Windows 2000 or later).
MediaPlayPause The media play pause key (Windows 2000 or later).
LaunchMail The launch mail key (Windows 2000 or later).
SelectMedia The select media key (Windows 2000 or later).
LaunchApplication1 The start application one key (Windows 2000 or later).
LaunchApplication2 The start application two key (Windows 2000 or later).
OemSemicolon The OEM Semicolon key on a US standard keyboard (Windows 2000 or later).
Oem1 The OEM 1 key.
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).
OemQuestion The OEM question mark key on a US standard keyboard (Windows 2000 or later).
Oem2 The OEM 2 key.
Oemtilde The OEM tilde key on a US standard keyboard (Windows 2000 or later).
Oem3 The OEM 3 key.
OemOpenBrackets The OEM open bracket key on a US standard keyboard (Windows 2000 or later).
Oem4 The OEM 4 key.
OemPipe The OEM pipe key on a US standard keyboard (Windows 2000 or later).
Oem5 The OEM 5 key.
OemCloseBrackets The OEM close bracket key on a US standard keyboard (Windows 2000 or later).
Oem6 The OEM 6 key.
OemQuotes The OEM singled/double quote key on a US standard keyboard (Windows 2000 or later).
Oem7 The OEM 7 key.
Oem8 The OEM 8 key.
OemBackslash The OEM angle bracket or backslash key on the RT 102 key keyboard (Windows 2000 or later).
Oem102 The OEM 102 key.
Supported by the .NET Compact FrameworkProcessKey The PROCESS KEY key.
Packet Used to pass Unicode characters as if they were keystrokes. The Packet key value is the low word of a 32-bit virtual-key value used for non-keyboard input methods.
Supported by the .NET Compact FrameworkAttn The ATTN key.
Supported by the .NET Compact FrameworkCrsel The CRSEL key.
Supported by the .NET Compact FrameworkExsel The EXSEL key.
Supported by the .NET Compact FrameworkEraseEof The ERASE EOF key.
Supported by the .NET Compact FrameworkPlay The PLAY key.
Supported by the .NET Compact FrameworkZoom The ZOOM key.
Supported by the .NET Compact FrameworkNoName A constant reserved for future use.
Supported by the .NET Compact FrameworkPa1 The PA1 key.
Supported by the .NET Compact FrameworkOemClear The CLEAR key.
Supported by the .NET Compact FrameworkShift The SHIFT modifier key.
Supported by the .NET Compact FrameworkControl The CTRL modifier key.
Supported by the .NET Compact FrameworkAlt The ALT modifier key.

The Keys class contains constants for processing keyboard input. The members of the Keys enumeration consist of a key code and a set of modifiers combined into a single integer value. In the Win32 application programming interface (API) a key value has two halves, with the high-order bits containing the key code (which is the same as a Windows virtual key code), and the low-order bits representing key modifiers such as the SHIFT, CONTROL, and ALT keys.

NoteNote:

This enumeration provides no way to test whether the CAPS LOCK or NUM LOCK keys are currently activated. You can use one of the following techniques to determine if these keys are activated:

  • Call the IsKeyLocked method of the Control class.

  • For finer control, use the Win32 API functions GetKeyState, GetAsyncKeyState, or GetKeyboardState defined in user32.dll, to do this. For more information about calling native functions, see Consuming Unmanaged DLL Functions.

The following table shows the key code values represented by two enumerated values, representing both the general original equipment manufacturer (OEM) keys and the more specific U.S.-keyboard associations.

Hexadecimal value

U.S. keyboard

General OEM

BA

OemSemicolon

Oem1

BF

OemQuestion

Oem2

C0

Oemtilde

Oem3

DB

OemOpenBrackets

Oem4

DC

OemPipe

Oem5

DD

OemCloseBrackets

Oem6

DE

OemQuotes

Oem7

E2

OemBackslash

Oem102

Caution noteCaution:

For the .NET Framework 2.0, a member IMEAccept was added that supersedes the previous entry, IMEAceept, which was spelled incorrectly. The older version has been retained for backward compatibility, but it may be deleted in future versions of the .NET Framework

The following code example uses the KeyDown event to determine the type of character entered into the control.

Visual Basic
' Boolean flag used to determine when a character other than a number is entered.
Private nonNumberEntered As Boolean = False


' Handle the KeyDown event to determine the type of character entered into the control.
Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
     Handles textBox1.KeyDown
    ' Initialize the flag to false.
    nonNumberEntered = False

    ' Determine whether the keystroke is a number from the top of the keyboard.
    If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
        ' Determine whether the keystroke is a number from the keypad.
        If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
            ' Determine whether the keystroke is a backspace.
            If e.KeyCode <> Keys.Back Then
                ' A non-numerical keystroke was pressed. 
                ' Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = True
            End If
        End If
    End If
    'If shift key was pressed, it's not a number.
    If Control.ModifierKeys = Keys.Shift Then
        nonNumberEntered = true
    End If
End Sub 'textBox1_KeyDown


' This event occurs after the KeyDown event and can be used 
' to prevent characters from entering the control.
Private Sub textBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) _
    Handles textBox1.KeyPress
    ' Check for the flag being set in the KeyDown event.
    If nonNumberEntered = True Then
        ' Stop the character from being entered into the control since it is non-numerical.
        e.Handled = True
    End If
End Sub 'textBox1_KeyPress

C#
// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    {
        // Determine whether the keystroke is a number from the keypad.
        if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
        {
            // Determine whether the keystroke is a backspace.
            if(e.KeyCode != Keys.Back)
            {
                // A non-numerical keystroke was pressed.
                // Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;
            }
        }
    }
    //If shift key was pressed, it's not a number.
    if (Control.ModifierKeys == Keys.Shift) {
        nonNumberEntered = true;
    }
}

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
    {
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;
    }
}


Visual C++
   // Boolean flag used to determine when a character other than a number is entered.
private:
   bool nonNumberEntered;

   // Handle the KeyDown event to determine the type of character entered into the control.
   void textBox1_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
   {
      // Initialize the flag to false.
      nonNumberEntered = false;

      // Determine whether the keystroke is a number from the top of the keyboard.
      if ( e->KeyCode < Keys::D0 || e->KeyCode > Keys::D9 )
      {
         // Determine whether the keystroke is a number from the keypad.
         if ( e->KeyCode < Keys::NumPad0 || e->KeyCode > Keys::NumPad9 )
         {
            // Determine whether the keystroke is a backspace.
            if ( e->KeyCode != Keys::Back )
            {
               // A non-numerical keystroke was pressed.
               // Set the flag to true and evaluate in KeyPress event.
               nonNumberEntered = true;
            }
         }
      }
      //If shift key was pressed, it's not a number.
      if (Control::ModifierKeys == Keys::Shift) {
         nonNumberEntered = true;
      }
   }

   // This event occurs after the KeyDown event and can be used to prevent
   // characters from entering the control.
   void textBox1_KeyPress( Object^ /*sender*/, System::Windows::Forms::KeyPressEventArgs^ e )
   {
      // Check for the flag being set in the KeyDown event.
      if ( nonNumberEntered == true )
      {         // Stop the character from being entered into the control since it is non-numerical.
         e->Handled = true;
      }
   }

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, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

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, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Community Content   What is Community Content?
Add new content RSS  Annotations
Compact Framework Tip      Patrick Johnson   |   Edit   |   Show History

Imports System.Windows.Forms

That is where the enum lives. Don't forget to add it. or be explicit:

if e.KeyCode = System.Windows.Forms.Keys.Enter then...

Also watch out for Enter not equaling Enter, on Symbol 9094s the "Enter Key" is sending a 134 value (F23) not 13 as one might expect.

How to use Keys.KeyCode      FremyCompany   |   Edit   |   Show History
  

Dim keyCode As Keys = CType(m.WParam, Keys) And Keys.KeyCode

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker