Keys Enum (System - Windows.Forms) - Microsoft Docs
Keys Enum (System - Windows.Forms) - Microsoft Docs
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms.dll
This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its
member values.
In this article
Definition
Fields
Examples
Remarks
Applies to
See also
C# = Copy
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.KeysConverter)
)]
[System.Flags]
public enum Keys
Fields
A 65 The A key.
B 66 The B key.
C 67 The C key.
D 68 The D key.
D0 48 The 0 key.
D1 49 The 1 key.
D2 50 The 2 key.
D3 51 The 3 key.
D4 52 The 4 key.
D5 53 The 5 key.
D6 54 The 6 key.
D7 55 The 7 key.
D8 56 The 8 key.
D9 57 The 9 key.
E 69 The E key.
F 70 The F key.
G 71 The G key.
H 72 The H key.
HanguelMode 21 The IME Hanguel mode key. (maintained for compatibility; use
HangulMode )
I 73 The I key.
J 74 The J key.
K 75 The K key.
KeyCode 65535 The bitmask to extract a key code from a key value.
L 76 The L key.
M 77 The M key.
N 78 The N key.
O 79 The O key.
OemBackslash 226 The OEM angle bracket or backslash key on the RT 102 key
keyboard.
P 80 The P key.
Packet 231 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.
Q 81 The Q key.
R 82 The R key.
S 83 The S key.
T 84 The T key.
U 85 The U key.
V 86 The V key.
W 87 The W key.
X 88 The X key.
Y 89 The Y key.
Z 90 The Z key.
Examples
The following code example uses the KeyDown event to determine the type of character
entered into the control.
C# = Copy
// Boolean flag used to determine when a character other than a number is en-
tered.
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 key-
board.
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;
}
}
Remarks
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.
2 Warning
Do not use the values in this enumeration for combined bitwise operations. The values
in the enumeration are not mutually exclusive.
7 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:
For finer control, use the Windows 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.
BA OemSemicolon Oem1
BF OemQuestion Oem2
C0 Oemtilde Oem3
DB OemOpenBrackets Oem4
DC OemPipe Oem5
DD OemCloseBrackets Oem6
DE OemQuotes Oem7
E2 OemBackslash Oem102
U 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
Applies to
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
See also
KeysConverter
SendKeys
ConsoleKey
Shortcut
ModifierKeys
IsKeyLocked(Keys)
KeyEventArgs