語言

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
繼承
屬性

欄位

名稱 值 Description
Modifiers -65536

位元遮罩用來從鍵值中提取修飾符。

None 0

沒有按鍵。

LButton 1

滑鼠左鍵。

RButton 2

右滑鼠按鈕。

Cancel 3

取消鍵。

MButton 4

中鍵(三鍵滑鼠)。

XButton1 5

第一個 x 鍵滑鼠按鈕(五鍵滑鼠)。

XButton2 6

第二個 X 鍵(五鍵滑鼠)。

Back 8

退格鍵

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

大寫鎖匙。

CapsLock 20

大寫鎖匙。

HanguelMode 21

以我經驗,Hanguel 模式的鑰匙。 (為相容性維護;使用 HangulMode)

HangulMode 21

IME 是韓文模式的鑰匙。

KanaMode 21

以我經驗,就是假名模式的鑰匙。

JunjaMode 23

以我經驗,Junja模式的鑰匙。

FinalMode 24

IME 最終模式的鑰匙。

HanjaMode 25

以我經驗,Hanja 模式的鑰匙。

KanjiMode 25

以我經驗,漢字模式的鑰匙。

Escape 27

ESC 鍵

IMEConvert 28

IME 轉換金鑰。

IMENonconvert 29

以我經驗,非轉換金鑰。

IMEAccept 30

IME 接受金鑰,替換 IMEAceept。

IMEAceept 30

IME 的 accept key。 過時的,就用 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

就是 UP ARROW 鍵。

Right 39

右箭頭鍵。

Down 40

下箭頭鍵。

Select 41

SELECT 鍵。

Print 42

列印鍵。

Execute 43

執行鍵。

PrintScreen 44

列印畫面鍵。

Snapshot 44

列印畫面鍵。

Insert 45

INS的鑰匙。

Delete 46

DEL鑰匙。

Help 47

幫助鍵。

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 Natural Keyboard)。

RWin 92

右側 Windows 標誌鍵(Microsoft Natural Keyboard)。

Apps 93

應用程式鍵(Microsoft Natural Keyboard)。

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

原廠1號金鑰。

OemSemicolon 186

美國標準鍵盤上的原廠分號鍵。

Oemplus 187

任何國家/地區鍵盤上的 OEM Plus 鍵。

Oemcomma 188

任何國家/地區鍵盤上的原廠逗號鍵。

OemMinus 189

任何國家/地區鍵盤上的原廠減號鍵。

OemPeriod 190

任何國家/地區鍵盤上的原廠時期鍵。

Oem2 191

原廠 2 金鑰。

OemQuestion 191

美國標準鍵盤上的原廠問號鍵。

Oem3 192

OEM 3 鑰匙。

Oemtilde 192

美國標準鍵盤上的原廠波浪號鍵。

Oem4 219

原廠4號鑰匙。

OemOpenBrackets 219

美國標準鍵盤上的原廠開括號鍵。

Oem5 220

原廠 5 鑰匙。

OemPipe 220

美國標準鍵盤上的原廠管鍵。

Oem6 221

原廠6鑰匙。

OemCloseBrackets 221

美國標準鍵盤上的原廠閉合括號鍵。

Oem7 222

原廠7號鑰匙。

OemQuotes 222

美國標準鍵盤上的原廠單引號/雙引號鍵。

Oem8 223

OEM 8 鑰匙。

Oem102 226

原廠102鑰匙。

OemBackslash 226

RT 102 鍵盤上的原廠角括號或反斜線鍵。

ProcessKey 229

PROCESS鍵。

Packet 231

用來傳遞 Unicode 字元,就像是擊鍵一樣。 封包金鑰值是用於非鍵盤輸入方法的 32 位元虛擬金鑰值的最低字。

Attn 246

ATTN 金鑰。

Crsel 247

CRSEL 金鑰。

Exsel 248

EXSEL的鑰匙。

EraseEof 249

刪除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 類別包含用於處理鍵盤輸入的常數。 鍵列舉的成員由一個鍵碼和一組修飾符組成,合併成一個整數值。 在 Win32 應用程式介面(API)中,鍵值分為兩半,高階位元包含鍵碼(與 Windows 虛擬鍵碼相同),低階位元代表鍵修飾鍵,如 SHIFT、CONTROL 和 ALT 鍵。

警告

不要將此列舉中的值用於組合的位元運算。 列舉中的數值並非互斥。

Note

此列舉無法測試 CAPS LOCK 或 NUM LOCK 鍵目前是否啟用。 您可以使用以下其中一種方法來判斷這些金鑰是否被啟動:

  • 呼叫 IsKeyLocked 該 Control 類別的方法。

  • 若要更細緻的控制,可以使用 user32.dll中定義的Windows API 函式GetKeyState、GetAsyncKeyState 或 GetKeyboardState。 欲了解更多關於呼叫原生函式的資訊,請參閱 「消費未受管理的 DLL 函式」。

下表顯示由兩個列舉值代表的鍵碼值,既代表一般原始設備製造商(OEM)鍵,也代表更具體的美國鍵盤關聯。

十六進位值 美國鍵盤 一般原廠製造商
文学士 OemSemicolon 原廠1
男友 / 好朋友 OEM問題 原廠2
C0 奧姆蒂爾德 原廠3
DB OemOpenBrackets OEM4
DC OemPipe 原廠5
DD 原廠關閉括號 Oem6
德 原廠名言 OEM7
E2 原廠回放斜線 OEM102

注意事項

在 Framework 2.0 .NET,新增了一個成員 IMEAccept,取代了先前拼寫錯誤的 IMEAceept。 舊版本為了向下相容而保留,但未來的 .NET 版本可能會被刪除。

適用於

另請參閱