Export (0) Print
Expand All
Expand Minimize
2 out of 6 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 nameDescription
KeyCodeThe bitmask to extract a key code from a key value.
ModifiersThe bitmask to extract modifiers from a key value.
NoneNo key pressed.
LButtonThe left mouse button.
RButtonThe right mouse button.
CancelThe CANCEL key.
MButtonThe middle mouse button (three-button mouse).
XButton1The first x mouse button (five-button mouse).
XButton2The second x mouse button (five-button mouse).
BackThe BACKSPACE key.
TabThe TAB key.
LineFeedThe LINEFEED key.
ClearThe CLEAR key.
ReturnThe RETURN key.
EnterThe ENTER key.
ShiftKeyThe SHIFT key.
ControlKeyThe CTRL key.
MenuThe ALT key.
PauseThe PAUSE key.
CapitalThe CAPS LOCK key.
CapsLockThe CAPS LOCK key.
KanaModeThe IME Kana mode key.
HanguelModeThe IME Hanguel mode key. (maintained for compatibility; use HangulMode)
HangulModeThe IME Hangul mode key.
JunjaModeThe IME Junja mode key.
FinalModeThe IME final mode key.
HanjaModeThe IME Hanja mode key.
KanjiModeThe IME Kanji mode key.
EscapeThe ESC key.
IMEConvertThe IME convert key.
IMENonconvertThe IME nonconvert key.
IMEAcceptThe IME accept key, replaces IMEAceept.
IMEAceeptThe IME accept key. Obsolete, use IMEAccept instead.
IMEModeChangeThe IME mode change key.
SpaceThe SPACEBAR key.
PriorThe PAGE UP key.
PageUpThe PAGE UP key.
NextThe PAGE DOWN key.
PageDownThe PAGE DOWN key.
EndThe END key.
HomeThe HOME key.
LeftThe LEFT ARROW key.
UpThe UP ARROW key.
RightThe RIGHT ARROW key.
DownThe DOWN ARROW key.
SelectThe SELECT key.
PrintThe PRINT key.
ExecuteThe EXECUTE key.
SnapshotThe PRINT SCREEN key.
PrintScreenThe PRINT SCREEN key.
InsertThe INS key.
DeleteThe DEL 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.
LWinThe left Windows logo key (Microsoft Natural Keyboard).
RWinThe right Windows logo key (Microsoft Natural Keyboard).
AppsThe 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.
NumLockThe NUM LOCK key.
ScrollThe SCROLL LOCK key.
LShiftKeyThe left SHIFT key.
RShiftKeyThe right SHIFT key.
LControlKeyThe left CTRL key.
RControlKeyThe right CTRL key.
LMenuThe left ALT key.
RMenuThe right ALT 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 (Windows 2000 or later).
VolumeDownThe volume down key (Windows 2000 or later).
VolumeUpThe volume up key (Windows 2000 or later).
MediaNextTrackThe media next track key (Windows 2000 or later).
MediaPreviousTrackThe media previous track key (Windows 2000 or later).
MediaStopThe media Stop key (Windows 2000 or later).
MediaPlayPauseThe media play pause key (Windows 2000 or later).
LaunchMailThe launch mail key (Windows 2000 or later).
SelectMediaThe select media key (Windows 2000 or later).
LaunchApplication1The start application one key (Windows 2000 or later).
LaunchApplication2The start application two key (Windows 2000 or later).
OemSemicolonThe OEM Semicolon key on a US standard keyboard (Windows 2000 or later).
Oem1The OEM 1 key.
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).
OemQuestionThe OEM question mark key on a US standard keyboard (Windows 2000 or later).
Oem2The OEM 2 key.
OemtildeThe OEM tilde key on a US standard keyboard (Windows 2000 or later).
Oem3The OEM 3 key.
OemOpenBracketsThe OEM open bracket key on a US standard keyboard (Windows 2000 or later).
Oem4The OEM 4 key.
OemPipeThe OEM pipe key on a US standard keyboard (Windows 2000 or later).
Oem5The OEM 5 key.
OemCloseBracketsThe OEM close bracket key on a US standard keyboard (Windows 2000 or later).
Oem6The OEM 6 key.
OemQuotesThe OEM singled/double quote key on a US standard keyboard (Windows 2000 or later).
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).
Oem102The OEM 102 key.
ProcessKeyThe PROCESS KEY 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.
AttnThe ATTN key.
CrselThe CRSEL key.
ExselThe EXSEL key.
EraseEofThe ERASE EOF key.
PlayThe PLAY key.
ZoomThe ZOOM key.
NoNameA constant reserved for future use.
Pa1The PA1 key.
OemClearThe CLEAR key.
ShiftThe SHIFT modifier key.
ControlThe CTRL modifier key.
AltThe 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 noteCaution

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

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.


        // 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?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.