Keys 枚举

定义

指定键代码和修饰符。Specifies key codes and modifiers.

此枚举有一个 <a href="https://docs.microsoft.com/en-us/dotnet/api/system.flagsattribute">FlagsAttribute</a> 属性,允许按位组合成员值。

public enum class Keys
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.KeysConverter))]
[System.Flags]
[System.Runtime.InteropServices.ComVisible(true)]
public enum Keys
type Keys = 
Public Enum Keys
继承
属性

字段

A 65

A 键。The A key.

Add 107

加号键。The add key.

Alt 262144

Alt 修改键。The ALT modifier key.

Apps 93

应用程序键(Microsoft Natural Keyboard,人体工程学键盘)。The application key (Microsoft Natural Keyboard).

Attn 246

ATTN 键。The ATTN key.

B 66

B 键。The B key.

Back 8

BACKSPACE 键。The BACKSPACE key.

BrowserBack 166

浏览器后退键。The browser back key.

BrowserFavorites 171

浏览器收藏键。The browser favorites key.

BrowserForward 167

浏览器前进键。The browser forward key.

BrowserHome 172

浏览器主页键。The browser home key.

BrowserRefresh 168

浏览器刷新键。The browser refresh key.

BrowserSearch 170

浏览器搜索键。The browser search key.

BrowserStop 169

浏览器停止键。The browser stop key.

C 67

C 键。The C key.

Cancel 3

Cancel 键。The CANCEL key.

Capital 20

CAPS LOCK 键。The CAPS LOCK key.

CapsLock 20

CAPS LOCK 键。The CAPS LOCK key.

Clear 12

CLEAR 键。The CLEAR key.

Control 131072

Ctrl 修改键。The CTRL modifier key.

ControlKey 17

CTRL 键。The CTRL key.

Crsel 247

CRSEL 键。The CRSEL key.

D 68

D 键。The D key.

D0 48

0 键。The 0 key.

D1 49

1 键。The 1 key.

D2 50

2 键。The 2 key.

D3 51

3 键。The 3 key.

D4 52

4 键。The 4 key.

D5 53

5 键。The 5 key.

D6 54

6 键。The 6 key.

D7 55

7 键。The 7 key.

D8 56

8 键。The 8 key.

D9 57

9 键。The 9 key.

Decimal 110

句点键。The decimal key.

Delete 46

DEL 键。The DEL key.

Divide 111

除号键。The divide key.

Down 40

DOWN ARROW 键。The DOWN ARROW key.

E 69

E 键。The E key.

End 35

END 键。The END key.

Enter 13

ENTER 键。The ENTER key.

EraseEof 249

ERASE EOF 键。The ERASE EOF key.

Escape 27

ESC 键。The ESC key.

Execute 43

EXECUTE 键。The EXECUTE key.

Exsel 248

EXSEL 键。The EXSEL key.

F 70

F 键。The F key.

F1 112

F1 键。The F1 key.

F10 121

F10 键。The F10 key.

F11 122

F11 键。The F11 key.

F12 123

F12 键。The F12 key.

F13 124

F13 键。The F13 key.

F14 125

F14 键。The F14 key.

F15 126

F15 键。The F15 key.

F16 127

F16 键。The F16 key.

F17 128

F17 键。The F17 key.

F18 129

F18 键。The F18 key.

F19 130

F19 键。The F19 key.

F2 113

F2 键。The F2 key.

F20 131

F20 键。The F20 key.

F21 132

F21 键。The F21 key.

F22 133

F22 键。The F22 key.

F23 134

F23 键。The F23 key.

F24 135

F24 键。The F24 key.

F3 114

F3 键。The F3 key.

F4 115

F4 键。The F4 key.

F5 116

F5 键。The F5 key.

F6 117

F6 键。The F6 key.

F7 118

F7 键。The F7 key.

F8 119

F8 键。The F8 key.

F9 120

F9 键。The F9 key.

FinalMode 24

IME 最终模式键。The IME final mode key.

G 71

G 键。The G key.

H 72

H 键。The H key.

HanguelMode 21

IME Hanguel 模式键。The IME Hanguel mode key. (为了保持兼容性而设置;使用 HangulMode(maintained for compatibility; use HangulMode)

HangulMode 21

IME Hangul 模式键。The IME Hangul mode key.

HanjaMode 25

IME Hanja 模式键。The IME Hanja mode key.

Help 47

HELP 键。The HELP key.

Home 36

HOME 键。The HOME key.

I 73

I 键。The I key.

IMEAccept 30

IME 接受键,替换 IMEAceeptThe IME accept key, replaces IMEAceept.

IMEAceept 30

IME 接受键。The IME accept key. 已过时,请改用 IMEAcceptObsolete, use IMEAccept instead.

IMEConvert 28

IME 转换键。The IME convert key.

IMEModeChange 31

IME 模式更改键。The IME mode change key.

IMENonconvert 29

IME 非转换键。The IME nonconvert key.

Insert 45

INS 键。The INS key.

J 74

J 键。The J key.

JunjaMode 23

IME Junja 模式键。The IME Junja mode key.

K 75

K 键。The K key.

KanaMode 21

IME Kana 模式键。The IME Kana mode key.

KanjiMode 25

IME Kanji 模式键。The IME Kanji mode key.

KeyCode 65535

从键值提取键代码的位屏蔽。The bitmask to extract a key code from a key value.

L 76

L 键。The L key.

LaunchApplication1 182

启动应用程序一键。The start application one key.

LaunchApplication2 183

启动应用程序二键。The start application two key.

LaunchMail 180

启动邮件键。The launch mail key.

LButton 1

鼠标左按钮。The left mouse button.

LControlKey 162

左 CTRL 键。The left CTRL key.

Left 37

LEFT ARROW 键。The LEFT ARROW key.

LineFeed 10

LINEFEED 键。The LINEFEED key.

LMenu 164

左 ALT 键。The left ALT key.

LShiftKey 160

左 Shift 键。The left SHIFT key.

LWin 91

左 Windows 徽标键 (Microsoft Natural Keyboard)。The left Windows logo key (Microsoft Natural Keyboard).

M 77

M 键。The M key.

MButton 4

鼠标中按钮(三个按钮的鼠标)。The middle mouse button (three-button mouse).

MediaNextTrack 176

媒体下一曲目键。The media next track key.

MediaPlayPause 179

媒体播放暂停键。The media play pause key.

MediaPreviousTrack 177

媒体上一曲目键。The media previous track key.

MediaStop 178

媒体停止键。The media Stop key.

Menu 18

Alt 键。The ALT key.

Modifiers -65536

从键值提取修饰符的位屏蔽。The bitmask to extract modifiers from a key value.

Multiply 106

乘号键。The multiply key.

N 78

N 键。The N key.

Next 34

PAGE DOWN 键。The PAGE DOWN key.

NoName 252

留待将来使用的常数。A constant reserved for future use.

None 0

不按任何键。No key pressed.

NumLock 144

NUM LOCK 键。The NUM LOCK key.

NumPad0 96

数字键盘上的 0 键。The 0 key on the numeric keypad.

NumPad1 97

数字键盘上的 1 键。The 1 key on the numeric keypad.

NumPad2 98

数字键盘上的 2 键。The 2 key on the numeric keypad.

NumPad3 99

数字键盘上的 3 键。The 3 key on the numeric keypad.

NumPad4 100

数字键盘上的 4 键。The 4 key on the numeric keypad.

NumPad5 101

数字键盘上的 5 键。The 5 key on the numeric keypad.

NumPad6 102

数字键盘上的 6 键。The 6 key on the numeric keypad.

NumPad7 103

数字键盘上的 7 键。The 7 key on the numeric keypad.

NumPad8 104

数字键盘上的 8 键。The 8 key on the numeric keypad.

NumPad9 105

数字键盘上的 9 键。The 9 key on the numeric keypad.

O 79

O 键。The O key.

Oem1 186

OEM 1 键。The OEM 1 key.

Oem102 226

OEM 102 键。The OEM 102 key.

Oem2 191

OEM 2 键。The OEM 2 key.

Oem3 192

OEM 3 键。The OEM 3 key.

Oem4 219

OEM 4 键。The OEM 4 key.

Oem5 220

OEM 5 键。The OEM 5 key.

Oem6 221

OEM 6 键。The OEM 6 key.

Oem7 222

OEM 7 键。The OEM 7 key.

Oem8 223

OEM 8 键。The OEM 8 key.

OemBackslash 226

RT 102 键的键盘上的 OEM 尖括号或反斜杠键。The OEM angle bracket or backslash key on the RT 102 key keyboard.

OemClear 254

CLEAR 键。The CLEAR key.

OemCloseBrackets 221

美式标准键盘上的 OEM 右括号键。The OEM close bracket key on a US standard keyboard.

Oemcomma 188

任何国家/地区键盘上的 OEM 逗号键。The OEM comma key on any country/region keyboard.

OemMinus 189

任何国家/地区键盘上的 OEM 减号键。The OEM minus key on any country/region keyboard.

OemOpenBrackets 219

美式标准键盘上的 OEM 左括号键。The OEM open bracket key on a US standard keyboard.

OemPeriod 190

任何国家/地区键盘上的 OEM 句号键。The OEM period key on any country/region keyboard.

OemPipe 220

美式标准键盘上的 OEM 管道键。The OEM pipe key on a US standard keyboard.

Oemplus 187

任何国家/地区键盘上的 OEM 加号键。The OEM plus key on any country/region keyboard.

OemQuestion 191

美式标准键盘上的 OEM 问号键。The OEM question mark key on a US standard keyboard.

OemQuotes 222

美式标准键盘上的 OEM 单/双引号键。The OEM singled/double quote key on a US standard keyboard.

OemSemicolon 186

美式标准键盘上的 OEM 分号键。The OEM Semicolon key on a US standard keyboard.

Oemtilde 192

美式标准键盘上的 OEM 波形符键。The OEM tilde key on a US standard keyboard.

P 80

P 键。The P key.

Pa1 253

PA1 键。The PA1 key.

Packet 231

用于将 Unicode 字符当作键击传递。Used to pass Unicode characters as if they were keystrokes. Packet 键值是用于非键盘输入法的 32 位虚拟键值的低位字。The Packet key value is the low word of a 32-bit virtual-key value used for non-keyboard input methods.

PageDown 34

PAGE DOWN 键。The PAGE DOWN key.

PageUp 33

PAGE UP 键。The PAGE UP key.

Pause 19

PAUSE 键。The PAUSE key.

Play 250

播放键。The PLAY key.

Print 42

PRINT 键。The PRINT key.

PrintScreen 44

PRINT SCREEN 键。The PRINT SCREEN key.

Prior 33

PAGE UP 键。The PAGE UP key.

ProcessKey 229

Process Key 键。The PROCESS KEY key.

Q 81

Q 键。The Q key.

R 82

R 键。The R key.

RButton 2

鼠标右按钮。The right mouse button.

RControlKey 163

右 CTRL 键。The right CTRL key.

Return 13

Return 键。The RETURN key.

Right 39

RIGHT ARROW 键。The RIGHT ARROW key.

RMenu 165

右 ALT 键。The right ALT key.

RShiftKey 161

右 Shift 键。The right SHIFT key.

RWin 92

右 Windows 徽标键 (Microsoft Natural Keyboard)。The right Windows logo key (Microsoft Natural Keyboard).

S 83

S 键。The S key.

Scroll 145

Scroll Lock 键。The SCROLL LOCK key.

Select 41

SELECT 键。The SELECT key.

SelectMedia 181

选择媒体键。The select media key.

Separator 108

分隔符键。The separator key.

Shift 65536

Shift 修改键。The SHIFT modifier key.

ShiftKey 16

Shift 键。The SHIFT key.

Sleep 95

计算机睡眠键。The computer sleep key.

Snapshot 44

PRINT SCREEN 键。The PRINT SCREEN key.

Space 32

SPACEBAR 键。The SPACEBAR key.

Subtract 109

减号键。The subtract key.

T 84

T 键。The T key.

Tab 9

TAB 键。The TAB key.

U 85

U 键。The U key.

Up 38

UP ARROW 键。The UP ARROW key.

V 86

V 键。The V key.

VolumeDown 174

音量减小键。The volume down key.

VolumeMute 173

静音键。The volume mute key.

VolumeUp 175

音量增大键。The volume up key.

W 87

W 键。The W key.

X 88

X 键。The X key.

XButton1 5

第一个 X 鼠标按钮(五个按钮的鼠标)。The first x mouse button (five-button mouse).

XButton2 6

第二个 X 鼠标按钮(五个按钮的鼠标)。The second x mouse button (five-button mouse).

Y 89

Y 键。The Y key.

Z 90

Z 键。The Z key.

Zoom 251

缩放键。The ZOOM key.

示例

下面的代码示例使用 KeyDown 事件确定输入到控件中的字符的类型。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 == 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 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;
    }
}

 ' 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

注解

Keys 类包含用于处理键盘输入的常量。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. 在 Win32 应用程序编程接口(API)中,键值具有两部分,其中的高序位包含密钥代码(与 Windows 虚拟键代码相同),低序位表示键修饰符(如 SHIFT、CONTROL 和 ALT 键)。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.

警告

不要将此枚举中的值用于组合的按位运算。Do not use the values in this enumeration for combined bitwise operations. 枚举中的值不相互排斥。The values in the enumeration are not mutually exclusive.

备注

此枚举无法测试 Caps Lock 或 NUM LOCK 键当前是否已激活。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:

  • 调用 IsKeyLocked 类的 Control 方法。Call the IsKeyLocked method of the Control class.

  • 若要进行更精细的控制,请使用 GetKeyStateGetAsyncKeyState或在 mscoree.dll 中定义 GetKeyboardState 的 Windows API 函数来执行此操作。For finer control, use the Windows API functions GetKeyState, GetAsyncKeyState, or GetKeyboardState defined in user32.dll, to do this. 有关调用本机函数的详细信息,请参阅使用非托管 DLL 函数For more information about calling native functions, see Consuming Unmanaged DLL Functions.

下表显示了由两个枚举值表示的关键代码值,这些值表示常规原始设备制造商(OEM)密钥和更具体的 U.S. 键盘关联。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 一般 OEMGeneral OEM
BABA OemSemicolonOemSemicolon Oem1.infOem1
BFBF OemQuestionOemQuestion Oem2Oem2
C0C0 OemtildeOemtilde Oem3Oem3
DBDB OemOpenBracketsOemOpenBrackets Oem4Oem4
DCDC OemPipeOemPipe Oem5Oem5
DDDD OemCloseBracketsOemCloseBrackets Oem6Oem6
DEDE OemQuotesOemQuotes Oem7Oem7
E2E2 OemBackslashOemBackslash Oem102Oem102

注意

对于 .NET Framework 2.0,添加了一个取代了前面条目 IMEAceept 的成员 IMEAccept,该条目拼写错误。For the .NET Framework 2.0, a member IMEAccept was added that supersedes the previous entry, IMEAceept, which was spelled incorrectly. 为实现向后兼容性而保留了较旧的版本,但它可能会在将来的 .NET Framework 版本中删除。The older version has been retained for backward compatibility, but it may be deleted in future versions of the .NET Framework

适用于

另请参阅