Επεξεργασία

Κοινή χρήση μέσω


Keys Enum

Definition

Specifies key codes and modifiers.

This enumeration supports a bitwise combination of its member values.

public enum class Keys
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.KeysConverter))]
[System.Flags]
[System.Runtime.InteropServices.ComVisible(true)]
public enum Keys
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.KeysConverter))]
[System.Flags]
public enum Keys
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.KeysConverter))>]
[<System.Flags>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Keys = 
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.KeysConverter))>]
[<System.Flags>]
type Keys = 
Public Enum Keys
Inheritance
Attributes

Fields

Name Value Description
Modifiers -65536

The bitmask to extract modifiers from a key value.

None 0

No key pressed.

LButton 1

The left mouse button.

RButton 2

The right mouse button.

Cancel 3

The CANCEL key.

MButton 4

The middle mouse button (three-button mouse).

XButton1 5

The first x mouse button (five-button mouse).

XButton2 6

The second x mouse button (five-button mouse).

Back 8

The BACKSPACE key.

Tab 9

The TAB key.

LineFeed 10

The LINEFEED key.

Clear 12

The CLEAR key.

Enter 13

The ENTER key.

Return 13

The RETURN key.

ShiftKey 16

The SHIFT key.

ControlKey 17

The CTRL key.

Menu 18

The ALT key.

Pause 19

The PAUSE key.

Capital 20

The CAPS LOCK key.

CapsLock 20

The CAPS LOCK key.

HanguelMode 21

The IME Hanguel mode key. (maintained for compatibility; use HangulMode)

HangulMode 21

The IME Hangul mode key.

KanaMode 21

The IME Kana mode key.

JunjaMode 23

The IME Junja mode key.

FinalMode 24

The IME final mode key.

HanjaMode 25

The IME Hanja mode key.

KanjiMode 25

The IME Kanji mode key.

Escape 27

The ESC key.

IMEConvert 28

The IME convert key.

IMENonconvert 29

The IME nonconvert key.

IMEAccept 30

The IME accept key, replaces IMEAceept.

IMEAceept 30

The IME accept key. Obsolete, use IMEAccept instead.

IMEModeChange 31

The IME mode change key.

Space 32

The SPACEBAR key.

PageUp 33

The PAGE UP key.

Prior 33

The PAGE UP key.

Next 34

The PAGE DOWN key.

PageDown 34

The PAGE DOWN key.

End 35

The END key.

Home 36

The HOME key.

Left 37

The LEFT ARROW key.

Up 38

The UP ARROW key.

Right 39

The RIGHT ARROW key.

Down 40

The DOWN ARROW key.

Select 41

The SELECT key.

Print 42

The PRINT key.

Execute 43

The EXECUTE key.

PrintScreen 44

The PRINT SCREEN key.

Snapshot 44

The PRINT SCREEN key.

Insert 45

The INS key.

Delete 46

The DEL key.

Help 47

The HELP 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.

A 65

The A key.

B 66

The B key.

C 67

The C key.

D 68

The D key.

E 69

The E key.

F 70

The F key.

G 71

The G key.

H 72

The H key.

I 73

The I key.

J 74

The J key.

K 75

The K key.

L 76

The L key.

M 77

The M key.

N 78

The N key.

O 79

The O key.

P 80

The P key.

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.

LWin 91

The left Windows logo key (Microsoft Natural Keyboard).

RWin 92

The right Windows logo key (Microsoft Natural Keyboard).

Apps 93

The application key (Microsoft Natural Keyboard).

Sleep 95

The computer sleep key.

NumPad0 96

The 0 key on the numeric keypad.

NumPad1 97

The 1 key on the numeric keypad.

NumPad2 98

The 2 key on the numeric keypad.

NumPad3 99

The 3 key on the numeric keypad.

NumPad4 100

The 4 key on the numeric keypad.

NumPad5 101

The 5 key on the numeric keypad.

NumPad6 102

The 6 key on the numeric keypad.

NumPad7 103

The 7 key on the numeric keypad.

NumPad8 104

The 8 key on the numeric keypad.

NumPad9 105

The 9 key on the numeric keypad.

Multiply 106

The multiply key.

Add 107

The add key.

Separator 108

The separator key.

Subtract 109

The subtract key.

Decimal 110

The decimal key.

Divide 111

The divide key.

F1 112

The F1 key.

F2 113

The F2 key.

F3 114

The F3 key.

F4 115

The F4 key.

F5 116

The F5 key.

F6 117

The F6 key.

F7 118

The F7 key.

F8 119

The F8 key.

F9 120

The F9 key.

F10 121

The F10 key.

F11 122

The F11 key.

F12 123

The F12 key.

F13 124

The F13 key.

F14 125

The F14 key.

F15 126

The F15 key.

F16 127

The F16 key.

F17 128

The F17 key.

F18 129

The F18 key.

F19 130

The F19 key.

F20 131

The F20 key.

F21 132

The F21 key.

F22 133

The F22 key.

F23 134

The F23 key.

F24 135

The F24 key.

NumLock 144

The NUM LOCK key.

Scroll 145

The SCROLL LOCK key.

LShiftKey 160

The left SHIFT key.

RShiftKey 161

The right SHIFT key.

LControlKey 162

The left CTRL key.

RControlKey 163

The right CTRL key.

LMenu 164

The left ALT key.

RMenu 165

The right ALT key.

BrowserBack 166

The browser back key.

BrowserForward 167

The browser forward key.

BrowserRefresh 168

The browser refresh key.

BrowserStop 169

The browser stop key.

BrowserSearch 170

The browser search key.

BrowserFavorites 171

The browser favorites key.

BrowserHome 172

The browser home key.

VolumeMute 173

The volume mute key.

VolumeDown 174

The volume down key.

VolumeUp 175

The volume up key.

MediaNextTrack 176

The media next track key.

MediaPreviousTrack 177

The media previous track key.

MediaStop 178

The media Stop key.

MediaPlayPause 179

The media play pause key.

LaunchMail 180

The launch mail key.

SelectMedia 181

The select media key.

LaunchApplication1 182

The start application one key.

LaunchApplication2 183

The start application two key.

Oem1 186

The OEM 1 key.

OemSemicolon 186

The OEM Semicolon key on a US standard keyboard.

Oemplus 187

The OEM plus key on any country/region keyboard.

Oemcomma 188

The OEM comma key on any country/region keyboard.

OemMinus 189

The OEM minus key on any country/region keyboard.

OemPeriod 190

The OEM period key on any country/region keyboard.

Oem2 191

The OEM 2 key.

OemQuestion 191

The OEM question mark key on a US standard keyboard.

Oem3 192

The OEM 3 key.

Oemtilde 192

The OEM tilde key on a US standard keyboard.

Oem4 219

The OEM 4 key.

OemOpenBrackets 219

The OEM open bracket key on a US standard keyboard.

Oem5 220

The OEM 5 key.

OemPipe 220

The OEM pipe key on a US standard keyboard.

Oem6 221

The OEM 6 key.

OemCloseBrackets 221

The OEM close bracket key on a US standard keyboard.

Oem7 222

The OEM 7 key.

OemQuotes 222

The OEM singled/double quote key on a US standard keyboard.

Oem8 223

The OEM 8 key.

Oem102 226

The OEM 102 key.

OemBackslash 226

The OEM angle bracket or backslash key on the RT 102 key keyboard.

ProcessKey 229

The PROCESS KEY 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.

Attn 246

The ATTN key.

Crsel 247

The CRSEL key.

Exsel 248

The EXSEL key.

EraseEof 249

The ERASE EOF key.

Play 250

The PLAY key.

Zoom 251

The ZOOM key.

NoName 252

A constant reserved for future use.

Pa1 253

The PA1 key.

OemClear 254

The CLEAR key.

KeyCode 65535

The bitmask to extract a key code from a key value.

Shift 65536

The SHIFT modifier key.

Control 131072

The CTRL modifier key.

Alt 262144

The ALT modifier key.

Examples

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;

   // Handle the KeyDown event to determine the type of character entered into the control.
   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.
   void textBox1_KeyPress( Object^ /*sender*/, System::Windows::Forms::KeyPressEventArgs^ e )
   {
      // Check for the flag being set in the KeyDown event.
      if ( nonNumberEntered)
      {         // 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 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)
    {
        // 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 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
     'If shift key was pressed, it's not a number.
     If Control.ModifierKeys = Keys.Shift Then
         nonNumberEntered = true
     End If
 End Sub


 ' 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

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.

Warning

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

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 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.

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

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

See also