Keys 枚举

定义

指定密钥代码和修饰符。

此枚举支持其成员值的按位组合。

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
继承
属性

字段

名称 说明
Modifiers -65536

要从键值中提取修饰符的位掩码。

None 0

未按下任何键。

LButton 1

鼠标左键。

RButton 2

鼠标右键。

Cancel 3

CANCEL 键。

MButton 4

中间鼠标按钮(三键鼠标)。

XButton1 5

第一个 x 鼠标按钮(五键鼠标)。

XButton2 6

第二个 x 鼠标按钮(五键鼠标)。

Back 8

Backspace 键。

Tab 9

Tab 键。

LineFeed 10

LINEFEED 键。

Clear 12

CLEAR 键。

Enter 13

ENTER 键。

Return 13

RETURN 键。

ShiftKey 16

Shift 键。

ControlKey 17

Ctrl 键。

Menu 18

Alt 键。

Pause 19

Pause 键。

Capital 20

CAPS LOCK 键。

CapsLock 20

CAPS LOCK 键。

HanguelMode 21

IME 朝鲜文库尔模式键。 (保持兼容性;使用 HangulMode

HangulMode 21

IME 朝鲜文模式键。

KanaMode 21

IME Kana 模式键。

JunjaMode 23

IME Junja 模式键。

FinalMode 24

IME 最终模式键。

HanjaMode 25

IME Hanja 模式键。

KanjiMode 25

IME 汉字模式键。

Escape 27

Esc 键。

IMEConvert 28

IME 转换键。

IMENonconvert 29

IME 非转换键。

IMEAccept 30

IME 接受键,替换 IMEAceept

IMEAceept 30

IME 接受密钥。 已过时,请改用 IMEAccept

IMEModeChange 31

IME 模式更改键。

Space 32

空格键。

PageUp 33

Page Up 键。

Prior 33

Page Up 键。

Next 34

Page Down 键。

PageDown 34

Page Down 键。

End 35

End 键。

Home 36

Home 键。

Left 37

向左键。

Up 38

向上键。

Right 39

向右键。

Down 40

向下键。

Select 41

SELECT 键。

Print 42

PRINT 键。

Execute 43

EXECUTE 键。

PrintScreen 44

打印屏幕键。

Snapshot 44

打印屏幕键。

Insert 45

INS 键。

Delete 46

DEL 键。

Help 47

HELP 键。

D0 48

0 键。

D1 49

1 键。

D2 50

2 键。

D3 51

3 键。

D4 52

4 键。

D5 53

5 键。

D6 54

6 键。

D7 55

7 键。

D8 56

8 键。

D9 57

9 键。

A 65

A 键。

B 66

B 键。

C 67

C 键。

D 68

D 键。

E 69

E 键。

F 70

F 键。

G 71

G 键。

H 72

H 键。

I 73

I 键。

J 74

J 键。

K 75

K 键。

L 76

L 键。

M 77

M 键。

N 78

N 键。

O 79

O 键。

P 80

P 键。

Q 81

Q 键。

R 82

R 键。

S 83

S 键。

T 84

T 键。

U 85

U 键。

V 86

V 键。

W 87

W 键。

X 88

X 键。

Y 89

Y 键。

Z 90

Z 键。

LWin 91

左侧Windows徽标键(Microsoft自然键盘)。

RWin 92

右Windows徽标键(Microsoft自然键盘)。

Apps 93

应用程序键(Microsoft自然键盘)。

Sleep 95

计算机睡眠键。

NumPad0 96

数字键盘上的 0 键。

NumPad1 97

数字键盘上的 1 键。

NumPad2 98

数字键盘上的 2 键。

NumPad3 99

数字键盘上的 3 个键。

NumPad4 100

数字键盘上的 4 键。

NumPad5 101

数字键盘上的 5 个键。

NumPad6 102

数字键盘上的 6 键。

NumPad7 103

数字键盘上的 7 键。

NumPad8 104

数字键盘上的 8 键。

NumPad9 105

数字键盘上的 9 键。

Multiply 106

乘键。

Add 107

添加键。

Separator 108

分隔符键。

Subtract 109

减去键。

Decimal 110

十进制键。

Divide 111

除号键。

F1 112

F1 键。

F2 113

F2 键。

F3 114

F3 键。

F4 115

F4 键。

F5 116

F5 键。

F6 117

F6 键。

F7 118

F7 键。

F8 119

F8 键。

F9 120

F9 键。

F10 121

F10 键。

F11 122

F11 键。

F12 123

F12 键。

F13 124

F13 键。

F14 125

F14 键。

F15 126

F15 键。

F16 127

F16 键。

F17 128

F17 键。

F18 129

F18 键。

F19 130

F19 键。

F20 131

F20 键。

F21 132

F21 键。

F22 133

F22 键。

F23 134

F23 键。

F24 135

F24 键。

NumLock 144

NUM LOCK 键。

Scroll 145

Scroll Lock 键。

LShiftKey 160

左 SHIFT 键。

RShiftKey 161

右 SHIFT 键。

LControlKey 162

左 Ctrl 键。

RControlKey 163

右 Ctrl 键。

LMenu 164

左 Alt 键。

RMenu 165

右 Alt 键。

BrowserBack 166

浏览器后退键。

BrowserForward 167

浏览器转发键。

BrowserRefresh 168

浏览器刷新密钥。

BrowserStop 169

浏览器停止键。

BrowserSearch 170

浏览器搜索键。

BrowserFavorites 171

浏览器收藏夹密钥。

BrowserHome 172

浏览器主键。

VolumeMute 173

音量静音键。

VolumeDown 174

音量调低键。

VolumeUp 175

卷向上键。

MediaNextTrack 176

媒体下一个跟踪键。

MediaPreviousTrack 177

媒体上一个跟踪键。

MediaStop 178

媒体停止键。

MediaPlayPause 179

媒体播放暂停键。

LaunchMail 180

启动邮件密钥。

SelectMedia 181

选择媒体键。

LaunchApplication1 182

启动应用程序一个密钥。

LaunchApplication2 183

启动应用程序两个密钥。

Oem1 186

OEM 1 键。

OemSemicolon 186

美国标准键盘上的 OEM 分号键。

Oemplus 187

任何国家/地区键盘上的 OEM 加键。

Oemcomma 188

任何国家/地区键盘上的 OEM 逗号键。

OemMinus 189

任何国家/地区键盘上的 OEM 减号键。

OemPeriod 190

任何国家/地区键盘上的 OEM 时间段键。

Oem2 191

OEM 2 键。

OemQuestion 191

美国标准键盘上的 OEM 问号键。

Oem3 192

OEM 3 键。

Oemtilde 192

美国标准键盘上的 OEM 平铺键。

Oem4 219

OEM 4 键。

OemOpenBrackets 219

美国标准键盘上的 OEM 打开方括号键。

Oem5 220

OEM 5 键。

OemPipe 220

美国标准键盘上的 OEM 管道键。

Oem6 221

OEM 6 键。

OemCloseBrackets 221

美国标准键盘上的 OEM 右括号键。

Oem7 222

OEM 7 键。

OemQuotes 222

美国标准键盘上的 OEM 单引号/双引号键。

Oem8 223

OEM 8 键。

Oem102 226

OEM 102 密钥。

OemBackslash 226

RT 102 键键盘上的 OEM 尖括号或反斜杠键。

ProcessKey 229

PROCESS KEY 密钥。

Packet 231

用于传递 Unicode 字符,就像是击键一样。 数据包键值是用于非键盘输入方法的 32 位虚拟键值的低字。

Attn 246

ATTN 键。

Crsel 247

CRSEL 键。

Exsel 248

EXSEL 键。

EraseEof 249

ERASE EOF 键。

Play 250

PLAY 键。

Zoom 251

ZOOM 键。

NoName 252

为将来使用保留的常量。

Pa1 253

PA1 键。

OemClear 254

CLEAR 键。

KeyCode 65535

要从键值中提取密钥代码的位掩码。

Shift 65536

SHIFT 修饰符键。

Control 131072

Ctrl 修饰符键。

Alt 262144

ALT 修饰符键。

示例

下面的代码示例使用 KeyDown 事件来确定输入控件中的字符的类型。

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

注解

Keys 类包含用于处理键盘输入的常量。 Keys 枚举的成员由键代码和一组修饰符组合成单个整数值组成。 在 Win32 应用程序编程接口(API)中,键值有两半,其中高阶位包含键代码(这与Windows虚拟密钥代码相同),以及表示键修饰符(如 SHIFT、CONTROL 和 ALT 键)的低序位。

Warning

不要将此枚举中的值用于组合位运算。 枚举中的值不是互斥的。

注释

此枚举无法测试 CAPS LOCK 或 NUM LOCK 键当前是否已激活。 可以使用以下方法之一来确定是否已激活这些密钥:

  • IsKeyLocked调用类的方法Control

  • 若要进行精细控制,请使用 user32.dll中定义的 Windows API 函数 GetKeyStateGetAsyncKeyStateGetKeyboardState 执行此操作。 有关调用本机函数的详细信息,请参阅 使用非托管 DLL 函数

下表显示了由两个枚举值表示的键代码值,表示常规原始设备制造商(OEM)键和更具体的美国键盘关联。

十六进制值 美国键盘 常规 OEM
BA OemSemicolon Oem1
BF OemQuestion Oem2
C0 Oemtilde Oem3
DB OemOpenBrackets Oem4
DC OemPipe Oem5
DD OemCloseBrackets Oem6
德国 OemQuotes Oem7
E2 OemBackslash Oem102

Caution

对于 .NET Framework 2.0,添加了一个成员 IMEAccept,该成员取代了上一项,IMEAceept拼写错误。 旧版本已保留,以实现向后兼容性,但在.NET的未来版本中可能会将其删除。

适用于

另请参阅