Export (0) Print
Expand All
Expand Minimize
2 out of 14 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] 
public enum Keys
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute FlagsAttribute() */ 
public enum Keys
ComVisibleAttribute(true) 
FlagsAttribute 
public enum Keys
 Member nameDescription
Supported by the .NET Compact FrameworkAThe A key. 
Supported by the .NET Compact FrameworkAddThe add key. 
Supported by the .NET Compact FrameworkAltThe ALT modifier key. 
Supported by the .NET Compact FrameworkAppsThe application key (Microsoft Natural Keyboard). 
Supported by the .NET Compact FrameworkAttnThe ATTN key. 
Supported by the .NET Compact FrameworkBThe B key. 
Supported by the .NET Compact FrameworkBackThe BACKSPACE key. 
BrowserBackThe browser back key (Windows 2000 or later). 
BrowserFavoritesThe browser favorites key (Windows 2000 or later). 
BrowserForwardThe browser forward key (Windows 2000 or later). 
BrowserHomeThe browser home key (Windows 2000 or later). 
BrowserRefreshThe browser refresh key (Windows 2000 or later). 
BrowserSearchThe browser search key (Windows 2000 or later). 
BrowserStopThe browser stop key (Windows 2000 or later). 
Supported by the .NET Compact FrameworkCThe C key. 
Supported by the .NET Compact FrameworkCancelThe CANCEL key. 
Supported by the .NET Compact FrameworkCapitalThe CAPS LOCK key. 
Supported by the .NET Compact FrameworkCapsLockThe CAPS LOCK key. 
Supported by the .NET Compact FrameworkClearThe CLEAR key. 
Supported by the .NET Compact FrameworkControlThe CTRL modifier key. 
Supported by the .NET Compact FrameworkControlKeyThe CTRL key. 
Supported by the .NET Compact FrameworkCrselThe CRSEL key. 
Supported by the .NET Compact FrameworkDThe D key. 
Supported by the .NET Compact FrameworkD0The 0 key. 
Supported by the .NET Compact FrameworkD1The 1 key. 
Supported by the .NET Compact FrameworkD2The 2 key. 
Supported by the .NET Compact FrameworkD3The 3 key. 
Supported by the .NET Compact FrameworkD4The 4 key. 
Supported by the .NET Compact FrameworkD5The 5 key. 
Supported by the .NET Compact FrameworkD6The 6 key. 
Supported by the .NET Compact FrameworkD7The 7 key. 
Supported by the .NET Compact FrameworkD8The 8 key. 
Supported by the .NET Compact FrameworkD9The 9 key. 
Supported by the .NET Compact FrameworkDecimalThe decimal key. 
Supported by the .NET Compact FrameworkDeleteThe DEL key. 
Supported by the .NET Compact FrameworkDivideThe divide key. 
Supported by the .NET Compact FrameworkDownThe DOWN ARROW key. 
Supported by the .NET Compact FrameworkEThe E key. 
Supported by the .NET Compact FrameworkEndThe END key. 
Supported by the .NET Compact FrameworkEnterThe ENTER key. 
Supported by the .NET Compact FrameworkEraseEofThe ERASE EOF key. 
Supported by the .NET Compact FrameworkEscapeThe ESC key. 
Supported by the .NET Compact FrameworkExecuteThe EXECUTE key. 
Supported by the .NET Compact FrameworkExselThe EXSEL key. 
Supported by the .NET Compact FrameworkFThe F key. 
Supported by the .NET Compact FrameworkF1The F1 key. 
Supported by the .NET Compact FrameworkF10The F10 key. 
Supported by the .NET Compact FrameworkF11The F11 key. 
Supported by the .NET Compact FrameworkF12The F12 key. 
Supported by the .NET Compact FrameworkF13The F13 key. 
Supported by the .NET Compact FrameworkF14The F14 key. 
Supported by the .NET Compact FrameworkF15The F15 key. 
Supported by the .NET Compact FrameworkF16The F16 key. 
Supported by the .NET Compact FrameworkF17The F17 key. 
Supported by the .NET Compact FrameworkF18The F18 key. 
Supported by the .NET Compact FrameworkF19The F19 key. 
Supported by the .NET Compact FrameworkF2The F2 key. 
Supported by the .NET Compact FrameworkF20The F20 key. 
Supported by the .NET Compact FrameworkF21The F21 key. 
Supported by the .NET Compact FrameworkF22The F22 key. 
Supported by the .NET Compact FrameworkF23The F23 key. 
Supported by the .NET Compact FrameworkF24The F24 key. 
Supported by the .NET Compact FrameworkF3The F3 key. 
Supported by the .NET Compact FrameworkF4The F4 key. 
Supported by the .NET Compact FrameworkF5The F5 key. 
Supported by the .NET Compact FrameworkF6The F6 key. 
Supported by the .NET Compact FrameworkF7The F7 key. 
Supported by the .NET Compact FrameworkF8The F8 key. 
Supported by the .NET Compact FrameworkF9The F9 key. 
FinalModeThe IME final mode key. 
Supported by the .NET Compact FrameworkGThe G key. 
Supported by the .NET Compact FrameworkHThe H key. 
HanguelModeThe IME Hanguel mode key. (maintained for compatibility; use HangulMode)  
HangulModeThe IME Hangul mode key. 
HanjaModeThe IME Hanja mode key. 
Supported by the .NET Compact FrameworkHelpThe HELP key. 
Supported by the .NET Compact FrameworkHomeThe HOME key. 
Supported by the .NET Compact FrameworkIThe I key. 
IMEAcceptThe IME accept key, replaces IMEAceept
IMEAceeptThe IME accept key. Obsolete, use IMEAccept instead. 
IMEConvertThe IME convert key. 
IMEModeChangeThe IME mode change key. 
IMENonconvertThe IME nonconvert key. 
Supported by the .NET Compact FrameworkInsertThe INS key. 
Supported by the .NET Compact FrameworkJThe J key. 
JunjaModeThe IME Junja mode key. 
Supported by the .NET Compact FrameworkKThe K key. 
KanaModeThe IME Kana mode key. 
KanjiModeThe IME Kanji mode key. 
Supported by the .NET Compact FrameworkKeyCodeThe bitmask to extract a key code from a key value. 
Supported by the .NET Compact FrameworkLThe L key. 
LaunchApplication1The start application one key (Windows 2000 or later). 
LaunchApplication2The start application two key (Windows 2000 or later). 
LaunchMailThe launch mail key (Windows 2000 or later). 
Supported by the .NET Compact FrameworkLButtonThe left mouse button. 
Supported by the .NET Compact FrameworkLControlKeyThe left CTRL key. 
Supported by the .NET Compact FrameworkLeftThe LEFT ARROW key. 
Supported by the .NET Compact FrameworkLineFeedThe LINEFEED key. 
Supported by the .NET Compact FrameworkLMenuThe left ALT key. 
Supported by the .NET Compact FrameworkLShiftKeyThe left SHIFT key. 
Supported by the .NET Compact FrameworkLWinThe left Windows logo key (Microsoft Natural Keyboard). 
Supported by the .NET Compact FrameworkMThe M key. 
Supported by the .NET Compact FrameworkMButtonThe middle mouse button (three-button mouse). 
MediaNextTrackThe media next track key (Windows 2000 or later). 
MediaPlayPauseThe media play pause key (Windows 2000 or later). 
MediaPreviousTrackThe media previous track key (Windows 2000 or later). 
MediaStopThe media Stop key (Windows 2000 or later). 
Supported by the .NET Compact FrameworkMenuThe ALT key. 
Supported by the .NET Compact FrameworkModifiersThe bitmask to extract modifiers from a key value. 
Supported by the .NET Compact FrameworkMultiplyThe multiply key. 
Supported by the .NET Compact FrameworkNThe N key. 
Supported by the .NET Compact FrameworkNextThe PAGE DOWN key. 
Supported by the .NET Compact FrameworkNoNameA constant reserved for future use. 
Supported by the .NET Compact FrameworkNoneNo key pressed. 
Supported by the .NET Compact FrameworkNumLockThe NUM LOCK key. 
Supported by the .NET Compact FrameworkNumPad0The 0 key on the numeric keypad. 
Supported by the .NET Compact FrameworkNumPad1The 1 key on the numeric keypad. 
Supported by the .NET Compact FrameworkNumPad2The 2 key on the numeric keypad. 
Supported by the .NET Compact FrameworkNumPad3The 3 key on the numeric keypad. 
Supported by the .NET Compact FrameworkNumPad4The 4 key on the numeric keypad. 
Supported by the .NET Compact FrameworkNumPad5The 5 key on the numeric keypad. 
Supported by the .NET Compact FrameworkNumPad6The 6 key on the numeric keypad. 
Supported by the .NET Compact FrameworkNumPad7The 7 key on the numeric keypad. 
Supported by the .NET Compact FrameworkNumPad8The 8 key on the numeric keypad. 
Supported by the .NET Compact FrameworkNumPad9The 9 key on the numeric keypad. 
Supported by the .NET Compact FrameworkOThe O key. 
Oem1The OEM 1 key. 
Oem102The OEM 102 key. 
Oem2The OEM 2 key. 
Oem3The OEM 3 key. 
Oem4The OEM 4 key. 
Oem5The OEM 5 key. 
Oem6The OEM 6 key. 
Oem7The OEM 7 key. 
Oem8The OEM 8 key. 
OemBackslashThe OEM angle bracket or backslash key on the RT 102 key keyboard (Windows 2000 or later). 
Supported by the .NET Compact FrameworkOemClearThe CLEAR key. 
OemCloseBracketsThe OEM close bracket key on a US standard 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). 
OemOpenBracketsThe OEM open bracket key on a US standard keyboard (Windows 2000 or later). 
OemPeriodThe OEM period key on any country/region keyboard (Windows 2000 or later). 
OemPipeThe OEM pipe key on a US standard keyboard (Windows 2000 or later). 
OemplusThe OEM plus key on any country/region keyboard (Windows 2000 or later). 
OemQuestionThe OEM question mark key on a US standard keyboard (Windows 2000 or later). 
OemQuotesThe OEM singled/double quote key on a US standard keyboard (Windows 2000 or later). 
OemSemicolonThe OEM Semicolon key on a US standard keyboard (Windows 2000 or later). 
OemtildeThe OEM tilde key on a US standard keyboard (Windows 2000 or later). 
Supported by the .NET Compact FrameworkPThe P key. 
Supported by the .NET Compact FrameworkPa1The PA1 key. 
PacketUsed 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 FrameworkPageDownThe PAGE DOWN key. 
Supported by the .NET Compact FrameworkPageUpThe PAGE UP key. 
Supported by the .NET Compact FrameworkPauseThe PAUSE key. 
Supported by the .NET Compact FrameworkPlayThe PLAY key. 
Supported by the .NET Compact FrameworkPrintThe PRINT key. 
Supported by the .NET Compact FrameworkPrintScreenThe PRINT SCREEN key. 
Supported by the .NET Compact FrameworkPriorThe PAGE UP key. 
Supported by the .NET Compact FrameworkProcessKeyThe PROCESS KEY key. 
Supported by the .NET Compact FrameworkQThe Q key. 
Supported by the .NET Compact FrameworkRThe R key. 
Supported by the .NET Compact FrameworkRButtonThe right mouse button. 
Supported by the .NET Compact FrameworkRControlKeyThe right CTRL key. 
Supported by the .NET Compact FrameworkReturnThe RETURN key. 
Supported by the .NET Compact FrameworkRightThe RIGHT ARROW key. 
Supported by the .NET Compact FrameworkRMenuThe right ALT key. 
Supported by the .NET Compact FrameworkRShiftKeyThe right SHIFT key. 
Supported by the .NET Compact FrameworkRWinThe right Windows logo key (Microsoft Natural Keyboard). 
Supported by the .NET Compact FrameworkSThe S key. 
Supported by the .NET Compact FrameworkScrollThe SCROLL LOCK key. 
Supported by the .NET Compact FrameworkSelectThe SELECT key. 
SelectMediaThe select media key (Windows 2000 or later). 
Supported by the .NET Compact FrameworkSeparatorThe separator key. 
Supported by the .NET Compact FrameworkShiftThe SHIFT modifier key. 
Supported by the .NET Compact FrameworkShiftKeyThe SHIFT key. 
SleepThe computer sleep key. 
Supported by the .NET Compact FrameworkSnapshotThe PRINT SCREEN key. 
Supported by the .NET Compact FrameworkSpaceThe SPACEBAR key. 
Supported by the .NET Compact FrameworkSubtractThe subtract key. 
Supported by the .NET Compact FrameworkTThe T key. 
Supported by the .NET Compact FrameworkTabThe TAB key. 
Supported by the .NET Compact FrameworkUThe U key. 
Supported by the .NET Compact FrameworkUpThe UP ARROW key. 
Supported by the .NET Compact FrameworkVThe V key. 
VolumeDownThe volume down key (Windows 2000 or later). 
VolumeMuteThe volume mute key (Windows 2000 or later). 
VolumeUpThe volume up key (Windows 2000 or later). 
Supported by the .NET Compact FrameworkWThe W key. 
Supported by the .NET Compact FrameworkXThe X key. 
Supported by the .NET Compact FrameworkXButton1The first x mouse button (five-button mouse). 
Supported by the .NET Compact FrameworkXButton2The second x mouse button (five-button mouse). 
Supported by the .NET Compact FrameworkYThe Y key. 
Supported by the .NET Compact FrameworkZThe Z key. 
Supported by the .NET Compact FrameworkZoomThe ZOOM 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:

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

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

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


// Boolean flag used to determine when a character other than a number 
// is entered.
private boolean 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.get_KeyCode().CompareTo(Keys.D0)) < 0 
        || (e.get_KeyCode().CompareTo(Keys.D9) > 0)) {

        // Determine whether the keystroke is a number from the keypad.
        if ((e.get_KeyCode().CompareTo(Keys.NumPad0) < 0 
            || (e.get_KeyCode().CompareTo(Keys.NumPad9)) > 0)) {

            // Determine whether the keystroke is a backspace.
            if (!(e.get_KeyCode().Equals(Keys.Back))) {
                    // A non-numerical keystroke was pressed.
                    // Set the flag to true and evaluate in KeyPress event.
                    nonNumberEntered = true;
            }
        }
    }
} //textBox1_KeyDown

// 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.set_Handled(true);
    }
} //textBox1_KeyPress

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.