Keys Enumeration
This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.
Namespace: System.Windows.FormsAssembly: System.Windows.Forms (in system.windows.forms.dll)
| Member name | Description | |
|---|---|---|
![]() | A | The A key. |
![]() | Add | The add key. |
![]() | Alt | The ALT modifier key. |
![]() | Apps | The application key (Microsoft Natural Keyboard). |
![]() | Attn | The ATTN key. |
![]() | B | The B key. |
![]() | Back | The BACKSPACE key. |
| BrowserBack | The browser back key (Windows 2000 or later). | |
| BrowserFavorites | The browser favorites key (Windows 2000 or later). | |
| BrowserForward | The browser forward key (Windows 2000 or later). | |
| BrowserHome | The browser home key (Windows 2000 or later). | |
| BrowserRefresh | The browser refresh key (Windows 2000 or later). | |
| BrowserSearch | The browser search key (Windows 2000 or later). | |
| BrowserStop | The browser stop key (Windows 2000 or later). | |
![]() | C | The C key. |
![]() | Cancel | The CANCEL key. |
![]() | Capital | The CAPS LOCK key. |
![]() | CapsLock | The CAPS LOCK key. |
![]() | Clear | The CLEAR key. |
![]() | Control | The CTRL modifier key. |
![]() | ControlKey | The CTRL key. |
![]() | Crsel | The CRSEL key. |
![]() | D | The D 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. |
![]() | Decimal | The decimal key. |
![]() | Delete | The DEL key. |
![]() | Divide | The divide key. |
![]() | Down | The DOWN ARROW key. |
![]() | E | The E key. |
![]() | End | The END key. |
![]() | Enter | The ENTER key. |
![]() | EraseEof | The ERASE EOF key. |
![]() | Escape | The ESC key. |
![]() | Execute | The EXECUTE key. |
![]() | Exsel | The EXSEL key. |
![]() | F | The F key. |
![]() | F1 | The F1 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. |
![]() | F2 | The F2 key. |
![]() | F20 | The F20 key. |
![]() | F21 | The F21 key. |
![]() | F22 | The F22 key. |
![]() | F23 | The F23 key. |
![]() | F24 | The F24 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. |
| FinalMode | The IME final mode key. | |
![]() | G | The G key. |
![]() | H | The H key. |
| HanguelMode | The IME Hanguel mode key. (maintained for compatibility; use HangulMode) | |
| HangulMode | The IME Hangul mode key. | |
| HanjaMode | The IME Hanja mode key. | |
![]() | Help | The HELP key. |
![]() | Home | The HOME key. |
![]() | I | The I key. |
| IMEAccept | The IME accept key, replaces IMEAceept. | |
| IMEAceept | The IME accept key. Obsolete, use IMEAccept instead. | |
| IMEConvert | The IME convert key. | |
| IMEModeChange | The IME mode change key. | |
| IMENonconvert | The IME nonconvert key. | |
![]() | Insert | The INS key. |
![]() | J | The J key. |
| JunjaMode | The IME Junja mode key. | |
![]() | K | The K key. |
| KanaMode | The IME Kana mode key. | |
| KanjiMode | The IME Kanji mode key. | |
![]() | KeyCode | The bitmask to extract a key code from a key value. |
![]() | L | The L key. |
| LaunchApplication1 | The start application one key (Windows 2000 or later). | |
| LaunchApplication2 | The start application two key (Windows 2000 or later). | |
| LaunchMail | The launch mail key (Windows 2000 or later). | |
![]() | LButton | The left mouse button. |
![]() | LControlKey | The left CTRL key. |
![]() | Left | The LEFT ARROW key. |
![]() | LineFeed | The LINEFEED key. |
![]() | LMenu | The left ALT key. |
![]() | LShiftKey | The left SHIFT key. |
![]() | LWin | The left Windows logo key (Microsoft Natural Keyboard). |
![]() | M | The M key. |
![]() | MButton | The middle mouse button (three-button mouse). |
| MediaNextTrack | The media next track key (Windows 2000 or later). | |
| MediaPlayPause | The media play pause key (Windows 2000 or later). | |
| MediaPreviousTrack | The media previous track key (Windows 2000 or later). | |
| MediaStop | The media Stop key (Windows 2000 or later). | |
![]() | Menu | The ALT key. |
![]() | Modifiers | The bitmask to extract modifiers from a key value. |
![]() | Multiply | The multiply key. |
![]() | N | The N key. |
![]() | Next | The PAGE DOWN key. |
![]() | NoName | A constant reserved for future use. |
![]() | None | No key pressed. |
![]() | NumLock | The NUM LOCK 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. |
![]() | O | The O key. |
| Oem1 | The OEM 1 key. | |
| Oem102 | The OEM 102 key. | |
| Oem2 | The OEM 2 key. | |
| Oem3 | The OEM 3 key. | |
| Oem4 | The OEM 4 key. | |
| Oem5 | The OEM 5 key. | |
| Oem6 | The OEM 6 key. | |
| 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). | |
![]() | OemClear | The CLEAR key. |
| OemCloseBrackets | The OEM close bracket key on a US standard 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). | |
| OemOpenBrackets | The OEM open bracket key on a US standard keyboard (Windows 2000 or later). | |
| OemPeriod | The OEM period key on any country/region keyboard (Windows 2000 or later). | |
| OemPipe | The OEM pipe key on a US standard keyboard (Windows 2000 or later). | |
| Oemplus | The OEM plus 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). | |
| OemQuotes | The OEM singled/double quote key on a US standard keyboard (Windows 2000 or later). | |
| OemSemicolon | The OEM Semicolon key on a US standard keyboard (Windows 2000 or later). | |
| Oemtilde | The OEM tilde key on a US standard keyboard (Windows 2000 or later). | |
![]() | P | The P key. |
![]() | Pa1 | The PA1 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. | |
![]() | PageDown | The PAGE DOWN key. |
![]() | PageUp | The PAGE UP key. |
![]() | Pause | The PAUSE key. |
![]() | Play | The PLAY key. |
![]() | The PRINT key. | |
![]() | PrintScreen | The PRINT SCREEN key. |
![]() | Prior | The PAGE UP key. |
![]() | ProcessKey | The PROCESS KEY key. |
![]() | Q | The Q key. |
![]() | R | The R key. |
![]() | RButton | The right mouse button. |
![]() | RControlKey | The right CTRL key. |
![]() | Return | The RETURN key. |
![]() | Right | The RIGHT ARROW key. |
![]() | RMenu | The right ALT key. |
![]() | RShiftKey | The right SHIFT key. |
![]() | RWin | The right Windows logo key (Microsoft Natural Keyboard). |
![]() | S | The S key. |
![]() | Scroll | The SCROLL LOCK key. |
![]() | Select | The SELECT key. |
| SelectMedia | The select media key (Windows 2000 or later). | |
![]() | Separator | The separator key. |
![]() | Shift | The SHIFT modifier key. |
![]() | ShiftKey | The SHIFT key. |
| Sleep | The computer sleep key. | |
![]() | Snapshot | The PRINT SCREEN key. |
![]() | Space | The SPACEBAR key. |
![]() | Subtract | The subtract key. |
![]() | T | The T key. |
![]() | Tab | The TAB key. |
![]() | U | The U key. |
![]() | Up | The UP ARROW key. |
![]() | V | The V key. |
| VolumeDown | The volume down key (Windows 2000 or later). | |
| VolumeMute | The volume mute key (Windows 2000 or later). | |
| VolumeUp | The volume up key (Windows 2000 or later). | |
![]() | W | The W key. |
![]() | X | The X key. |
![]() | XButton1 | The first x mouse button (five-button mouse). |
![]() | XButton2 | The second x mouse button (five-button mouse). |
![]() | Y | The Y key. |
![]() | Z | The Z key. |
![]() | Zoom | The 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.
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: |
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 | ||
| BF | ||
| C0 | ||
| DB | ||
| DC | ||
| DD | ||
| DE | ||
| E2 |
' 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
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
End Class 'Form1
// 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.
