6 out of 23 rated this helpful - Rate this topic

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)
[ComVisibleAttribute(true)]
[FlagsAttribute]
[TypeConverterAttribute(typeof(KeysConverter))]
public enum Keys
Member name Description
Supported by the .NET Compact Framework KeyCode The bitmask to extract a key code from a key value.
Supported by the .NET Compact Framework Modifiers The bitmask to extract modifiers from a key value.
Supported by the .NET Compact Framework None No key pressed.
Supported by the .NET Compact Framework LButton The left mouse button.
Supported by the .NET Compact Framework RButton The right mouse button.
Supported by the .NET Compact Framework Cancel The CANCEL key.
Supported by the .NET Compact Framework MButton The middle mouse button (three-button mouse).
Supported by the .NET Compact Framework XButton1 The first x mouse button (five-button mouse).
Supported by the .NET Compact Framework XButton2 The second x mouse button (five-button mouse).
Supported by the .NET Compact Framework Back The BACKSPACE key.
Supported by the .NET Compact Framework Tab The TAB key.
Supported by the .NET Compact Framework LineFeed The LINEFEED key.
Supported by the .NET Compact Framework Clear The CLEAR key.
Supported by the .NET Compact Framework Return The RETURN key.
Supported by the .NET Compact Framework Enter The ENTER key.
Supported by the .NET Compact Framework ShiftKey The SHIFT key.
Supported by the .NET Compact Framework ControlKey The CTRL key.
Supported by the .NET Compact Framework Menu The ALT key.
Supported by the .NET Compact Framework Pause The PAUSE key.
Supported by the .NET Compact Framework Capital The CAPS LOCK key.
Supported by the .NET Compact Framework CapsLock 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 Framework Escape 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 Framework Space The SPACEBAR key.
Supported by the .NET Compact Framework Prior The PAGE UP key.
Supported by the .NET Compact Framework PageUp The PAGE UP key.
Supported by the .NET Compact Framework Next The PAGE DOWN key.
Supported by the .NET Compact Framework PageDown The PAGE DOWN key.
Supported by the .NET Compact Framework End The END key.
Supported by the .NET Compact Framework Home The HOME key.
Supported by the .NET Compact Framework Left The LEFT ARROW key.
Supported by the .NET Compact Framework Up The UP ARROW key.
Supported by the .NET Compact Framework Right The RIGHT ARROW key.
Supported by the .NET Compact Framework Down The DOWN ARROW key.
Supported by the .NET Compact Framework Select The SELECT key.
Supported by the .NET Compact Framework Print The PRINT key.
Supported by the .NET Compact Framework Execute The EXECUTE key.
Supported by the .NET Compact Framework Snapshot The PRINT SCREEN key.
Supported by the .NET Compact Framework PrintScreen The PRINT SCREEN key.
Supported by the .NET Compact Framework Insert The INS key.
Supported by the .NET Compact Framework Delete The DEL key.
Supported by the .NET Compact Framework Help The HELP key.
Supported by the .NET Compact Framework D0 The 0 key.
Supported by the .NET Compact Framework D1 The 1 key.
Supported by the .NET Compact Framework D2 The 2 key.
Supported by the .NET Compact Framework D3 The 3 key.
Supported by the .NET Compact Framework D4 The 4 key.
Supported by the .NET Compact Framework D5 The 5 key.
Supported by the .NET Compact Framework D6 The 6 key.
Supported by the .NET Compact Framework D7 The 7 key.
Supported by the .NET Compact Framework D8 The 8 key.
Supported by the .NET Compact Framework D9 The 9 key.
Supported by the .NET Compact Framework A The A key.
Supported by the .NET Compact Framework B The B key.
Supported by the .NET Compact Framework C The C key.
Supported by the .NET Compact Framework D The D key.
Supported by the .NET Compact Framework E The E key.
Supported by the .NET Compact Framework F The F key.
Supported by the .NET Compact Framework G The G key.
Supported by the .NET Compact Framework H The H key.
Supported by the .NET Compact Framework I The I key.
Supported by the .NET Compact Framework J The J key.
Supported by the .NET Compact Framework K The K key.
Supported by the .NET Compact Framework L The L key.
Supported by the .NET Compact Framework M The M key.
Supported by the .NET Compact Framework N The N key.
Supported by the .NET Compact Framework O The O key.
Supported by the .NET Compact Framework P The P key.
Supported by the .NET Compact Framework Q The Q key.
Supported by the .NET Compact Framework R The R key.
Supported by the .NET Compact Framework S The S key.
Supported by the .NET Compact Framework T The T key.
Supported by the .NET Compact Framework U The U key.
Supported by the .NET Compact Framework V The V key.
Supported by the .NET Compact Framework W The W key.
Supported by the .NET Compact Framework X The X key.
Supported by the .NET Compact Framework Y The Y key.
Supported by the .NET Compact Framework Z The Z key.
Supported by the .NET Compact Framework LWin The left Windows logo key (Microsoft Natural Keyboard).
Supported by the .NET Compact Framework RWin The right Windows logo key (Microsoft Natural Keyboard).
Supported by the .NET Compact Framework Apps The application key (Microsoft Natural Keyboard).
Sleep The computer sleep key.
Supported by the .NET Compact Framework NumPad0 The 0 key on the numeric keypad.
Supported by the .NET Compact Framework NumPad1 The 1 key on the numeric keypad.
Supported by the .NET Compact Framework NumPad2 The 2 key on the numeric keypad.
Supported by the .NET Compact Framework NumPad3 The 3 key on the numeric keypad.
Supported by the .NET Compact Framework NumPad4 The 4 key on the numeric keypad.
Supported by the .NET Compact Framework NumPad5 The 5 key on the numeric keypad.
Supported by the .NET Compact Framework NumPad6 The 6 key on the numeric keypad.
Supported by the .NET Compact Framework NumPad7 The 7 key on the numeric keypad.
Supported by the .NET Compact Framework NumPad8 The 8 key on the numeric keypad.
Supported by the .NET Compact Framework NumPad9 The 9 key on the numeric keypad.
Supported by the .NET Compact Framework Multiply The multiply key.
Supported by the .NET Compact Framework Add The add key.
Supported by the .NET Compact Framework Separator The separator key.
Supported by the .NET Compact Framework Subtract The subtract key.
Supported by the .NET Compact Framework Decimal The decimal key.
Supported by the .NET Compact Framework Divide The divide key.
Supported by the .NET Compact Framework F1 The F1 key.
Supported by the .NET Compact Framework F2 The F2 key.
Supported by the .NET Compact Framework F3 The F3 key.
Supported by the .NET Compact Framework F4 The F4 key.
Supported by the .NET Compact Framework F5 The F5 key.
Supported by the .NET Compact Framework F6 The F6 key.
Supported by the .NET Compact Framework F7 The F7 key.
Supported by the .NET Compact Framework F8 The F8 key.
Supported by the .NET Compact Framework F9 The F9 key.
Supported by the .NET Compact Framework F10 The F10 key.
Supported by the .NET Compact Framework F11 The F11 key.
Supported by the .NET Compact Framework F12 The F12 key.
Supported by the .NET Compact Framework F13 The F13 key.
Supported by the .NET Compact Framework F14 The F14 key.
Supported by the .NET Compact Framework F15 The F15 key.
Supported by the .NET Compact Framework F16 The F16 key.
Supported by the .NET Compact Framework F17 The F17 key.
Supported by the .NET Compact Framework F18 The F18 key.
Supported by the .NET Compact Framework F19 The F19 key.
Supported by the .NET Compact Framework F20 The F20 key.
Supported by the .NET Compact Framework F21 The F21 key.
Supported by the .NET Compact Framework F22 The F22 key.
Supported by the .NET Compact Framework F23 The F23 key.
Supported by the .NET Compact Framework F24 The F24 key.
Supported by the .NET Compact Framework NumLock The NUM LOCK key.
Supported by the .NET Compact Framework Scroll The SCROLL LOCK key.
Supported by the .NET Compact Framework LShiftKey The left SHIFT key.
Supported by the .NET Compact Framework RShiftKey The right SHIFT key.
Supported by the .NET Compact Framework LControlKey The left CTRL key.
Supported by the .NET Compact Framework RControlKey The right CTRL key.
Supported by the .NET Compact Framework LMenu The left ALT key.
Supported by the .NET Compact Framework RMenu 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 Framework ProcessKey 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 Framework Attn The ATTN key.
Supported by the .NET Compact Framework Crsel The CRSEL key.
Supported by the .NET Compact Framework Exsel The EXSEL key.
Supported by the .NET Compact Framework EraseEof The ERASE EOF key.
Supported by the .NET Compact Framework Play The PLAY key.
Supported by the .NET Compact Framework Zoom The ZOOM key.
Supported by the .NET Compact Framework NoName A constant reserved for future use.
Supported by the .NET Compact Framework Pa1 The PA1 key.
Supported by the .NET Compact Framework OemClear The CLEAR key.
Supported by the .NET Compact Framework Shift The SHIFT modifier key.
Supported by the .NET Compact Framework Control The CTRL modifier key.
Supported by the .NET Compact Framework Alt 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.

Note Note:

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 note Caution:

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.

// 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;
    }
}



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
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Beware the FlagsAttribute on the enum!

The Keys enumeration has the FlagsAttribute, which allows binary (bitwise) OR operations between values. This help topic even starts by saying that this allows these values to be combined. You cannot combine key code enum values in this way!

The Flags attribute allows you to write code like:

if ((e.KeyCode & (Keys.Left | Keys.Right)) != 0)


... which is a nice and succinct test for e.KeyCode being one or other of the left and right arrow keys. Unfortunately, these enum symbols map onto (have values that are) the binary codes coming from the underlying keyboard subsystem, and those values are definitely mutually exclusive values (which is exactly not what Flags-mode enums are about) - so that code will not work. That code will test true for e.KeyCode == Keys.ControlKey - a false positive. (Here's why: Keys.Left is 0x25; Keys.Right is 0x27; Keys.ControlKey is 0x11; 0x11 & (0x25 | 0x27) == 0x01 ... which is not zero.)

In fact, the only values that can safely be combined are Keys.Shift, Keys.Control and Keys.Alt - whose values are the key code modifier values (0x00010000, 0x00020000 & 0x00040000, respectively).

How to use Keys.KeyCode
  

'VB Code

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

Compact Framework Tip

'VB code

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.