1 out of 4 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)
[FlagsAttribute]
[ComVisibleAttribute(true)]
[TypeConverterAttribute(typeof(KeysConverter))]
public enum Keys
Member name Description
KeyCode The bitmask to extract a key code from a key value.
Modifiers The bitmask to extract modifiers from a key value.
None No key pressed.
LButton The left mouse button.
RButton The right mouse button.
Cancel The CANCEL key.
MButton The middle mouse button (three-button mouse).
XButton1 The first x mouse button (five-button mouse).
XButton2 The second x mouse button (five-button mouse).
Back The BACKSPACE key.
Tab The TAB key.
LineFeed The LINEFEED key.
Clear The CLEAR key.
Return The RETURN key.
Enter The ENTER key.
ShiftKey The SHIFT key.
ControlKey The CTRL key.
Menu The ALT key.
Pause The PAUSE key.
Capital The CAPS LOCK key.
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.
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.
Space The SPACEBAR key.
Prior The PAGE UP key.
PageUp The PAGE UP key.
Next The PAGE DOWN key.
PageDown The PAGE DOWN key.
End The END key.
Home The HOME key.
Left The LEFT ARROW key.
Up The UP ARROW key.
Right The RIGHT ARROW key.
Down The DOWN ARROW key.
Select The SELECT key.
Print The PRINT key.
Execute The EXECUTE key.
Snapshot The PRINT SCREEN key.
PrintScreen The PRINT SCREEN key.
Insert The INS key.
Delete The DEL 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.
LWin The left Windows logo key (Microsoft Natural Keyboard).
RWin The right Windows logo key (Microsoft Natural Keyboard).
Apps 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.
NumLock The NUM LOCK key.
Scroll The SCROLL LOCK key.
LShiftKey The left SHIFT key.
RShiftKey The right SHIFT key.
LControlKey The left CTRL key.
RControlKey The right CTRL key.
LMenu The left ALT key.
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.
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.
Attn The ATTN key.
Crsel The CRSEL key.
Exsel The EXSEL key.
EraseEof 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.
Shift The SHIFT modifier key.
Control The CTRL modifier key.
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.

Caution note Caution

Do not use the values in this enumeration for combined bitwise operations. The values in the enumeration are not mutually exclusive.

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



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.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.
Did you find this helpful?
(2000 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).