Keys 列舉

定義

指定按鍵碼 (Key Code) 和修飾詞 (Modifier)。

此列舉支援其成員值的位元組合。

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

欄位

A 65

A 鍵。

Add 107

加號鍵。

Alt 262144

ALT 輔助按鍵 (Modifier Key)。

Apps 93

應用程式鍵 (Microsoft Natural Keyboard)。

Attn 246

ATTN 鍵。

B 66

B 鍵。

Back 8

退格鍵。

BrowserBack 166

瀏覽器上一頁鍵。

BrowserFavorites 171

瀏覽器我的最愛鍵。

BrowserForward 167

瀏覽器下一頁按鍵

BrowserHome 172

瀏覽器首頁鍵。

BrowserRefresh 168

瀏覽器重新整理鍵。

BrowserSearch 170

瀏覽器搜尋鍵。

BrowserStop 169

瀏覽器停止鍵。

C 67

C 鍵。

Cancel 3

CANCEL 鍵。

Capital 20

CAPS LOCK 鍵。

CapsLock 20

CAPS LOCK 鍵。

Clear 12

CLEAR 鍵。

Control 131072

CTRL 輔助按鍵。

ControlKey 17

CTRL 鍵。

Crsel 247

CRSEL 鍵。

D 68

D 鍵。

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 鍵。

Decimal 110

小數點鍵。

Delete 46

DEL 鍵。

Divide 111

除號鍵。

Down 40

向下鍵。

E 69

E 鍵。

End 35

END 鍵。

Enter 13

ENTER 鍵。

EraseEof 249

ERASE EOF 鍵。

Escape 27

ESC 鍵。

Execute 43

EXECUTE 鍵。

Exsel 248

EXSEL 鍵。

F 70

F 鍵。

F1 112

F1 鍵。

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 鍵。

F2 113

F2 鍵。

F20 131

F20 鍵。

F21 132

F21 鍵。

F22 133

F22 鍵。

F23 134

F23 鍵。

F24 135

F24 鍵。

F3 114

F3 鍵。

F4 115

F4 鍵。

F5 116

F5 鍵。

F6 117

F6 鍵。

F7 118

F7 鍵。

F8 119

F8 鍵。

F9 120

F9 鍵。

FinalMode 24

輸入法最終模式鍵。

G 71

G 鍵。

H 72

H 鍵。

HanguelMode 21

輸入法 Hanguel 模式鍵。 (為相容性而維持;使用 HangulMode)

HangulMode 21

輸入法 Hangul 模式鍵。

HanjaMode 25

輸入法 Hanja 模式鍵。

Help 47

HELP 鍵。

Home 36

HOME 鍵。

I 73

I 鍵。

IMEAccept 30

輸入法接受鍵,取代 IMEAceept

IMEAceept 30

輸入法接受鍵。 已經過時,請改用 IMEAccept

IMEConvert 28

輸入法轉換鍵。

IMEModeChange 31

輸入法模式變更鍵。

IMENonconvert 29

輸入法不轉換鍵。

Insert 45

INS 鍵。

J 74

J 鍵。

JunjaMode 23

輸入法 Junja 模式鍵。

K 75

K 鍵。

KanaMode 21

輸入法 Kana 模式鍵。

KanjiMode 25

輸入法 Kanji 模式鍵。

KeyCode 65535

從按鍵值中抽出按鍵碼的位元遮罩。

L 76

L 鍵。

LaunchApplication1 182

啟動應用程式一鍵。

LaunchApplication2 183

啟動應用程式二鍵。

LaunchMail 180

啟動郵件鍵。

LButton 1

滑鼠左鍵。

LControlKey 162

左 CTRL 鍵。

Left 37

向左鍵。

LineFeed 10

換行 (Linefeed) 鍵。

LMenu 164

左 ALT 鍵。

LShiftKey 160

左 SHIFT 鍵。

LWin 91

左 Windows 標誌鍵 (Microsoft Natural Keyboard)。

M 77

M 鍵。

MButton 4

滑鼠中鍵 (三鍵式滑鼠)。

MediaNextTrack 176

媒體下一首鍵。

MediaPlayPause 179

媒體播放暫停鍵。

MediaPreviousTrack 177

媒體上一首鍵。

MediaStop 178

媒體停止鍵。

Menu 18

ALT 鍵。

Modifiers -65536

從按鍵值中抽出修飾詞的位元遮罩。

Multiply 106

乘號鍵。

N 78

N 鍵。

Next 34

PAGE DOWN 鍵。

NoName 252

保留供未來使用的常數。

None 0

未按下任何鍵。

NumLock 144

NUM LOCK 鍵。

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 鍵。

O 79

O 鍵。

Oem1 186

OEM 1 鍵。

Oem102 226

OEM 102 鍵。

Oem2 191

OEM 2 鍵。

Oem3 192

OEM 3 鍵。

Oem4 219

OEM 4 鍵。

Oem5 220

OEM 5 鍵。

Oem6 221

OEM 6 鍵。

Oem7 222

OEM 7 鍵。

Oem8 223

OEM 8 鍵。

OemBackslash 226

RT 102 鍵盤上的 OEM 角括弧或反斜線鍵。

OemClear 254

CLEAR 鍵。

OemCloseBrackets 221

美式標準鍵盤上的 OEM 左括弧鍵。

Oemcomma 188

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

OemMinus 189

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

OemOpenBrackets 219

美式標準鍵盤上的 OEM 左括弧鍵。

OemPeriod 190

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

OemPipe 220

美式標準鍵盤上的 OEM 管道鍵。

Oemplus 187

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

OemQuestion 191

美式標準鍵盤上的 OEM 問號鍵。

OemQuotes 222

美式標準鍵盤上的 OEM 單/雙引號鍵。

OemSemicolon 186

美式標準鍵盤上的 OEM 分號鍵。

Oemtilde 192

美式標準鍵盤上的 OEM 波浪號鍵。

P 80

P 鍵。

Pa1 253

PA1 鍵。

Packet 231

用來將 Unicode 字元視為按鍵進行傳遞。 Packet 按鍵值用於非鍵盤輸入方法的 32 位元虛擬按鍵值低序位文字。

PageDown 34

PAGE DOWN 鍵。

PageUp 33

PAGE UP 鍵。

Pause 19

PAUSE 鍵。

Play 250

PLAY 鍵。

Print 42

PLAY 鍵。

PrintScreen 44

PRINT SCREEN 鍵。

Prior 33

PAGE UP 鍵。

ProcessKey 229

PROCESS KEY 鍵。

Q 81

Q 鍵。

R 82

R 鍵。

RButton 2

滑鼠右鍵。

RControlKey 163

右 CTRL 鍵。

Return 13

RETURN 鍵。

Right 39

向右鍵。

RMenu 165

右 ALT 鍵。

RShiftKey 161

右 SHIFT 鍵。

RWin 92

右 Windows 標誌鍵 (Microsoft Natural Keyboard)。

S 83

S 鍵。

Scroll 145

SCROLL LOCK 鍵。

Select 41

SELECT 鍵。

SelectMedia 181

選取媒體鍵。

Separator 108

分隔符號鍵。

Shift 65536

SHIFT 輔助按鍵。

ShiftKey 16

SHIFT 鍵。

Sleep 95

電腦休眠鍵。

Snapshot 44

PRINT SCREEN 鍵。

Space 32

空格鍵。

Subtract 109

減號鍵。

T 84

T 鍵。

Tab 9

TAB 鍵。

U 85

U 鍵。

Up 38

向上鍵。

V 86

V 鍵。

VolumeDown 174

音量降低鍵。

VolumeMute 173

靜音鍵。

VolumeUp 175

音量提高鍵。

W 87

W 鍵。

X 88

X 鍵。

XButton1 5

第一個 X 滑鼠鍵 (五鍵式滑鼠)。

XButton2 6

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

Y 89

Y 鍵。

Z 90

Z 鍵。

Zoom 251

ZOOM 鍵。

範例

下列程式碼範例會 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 == 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 包含用來處理鍵盤輸入的常數。 Keys 列舉的成員是由索引鍵程式碼和一組結合成單一整數值的修飾詞所組成。 在 Win32 應用程式程式設計介面中, (API) 索引鍵值有兩半,其中包含索引鍵程式碼 (的高階位,其與 Windows 虛擬金鑰程式碼) 相同,以及代表 SHIFT、CONTROL 和 ALT 鍵等金鑰修飾詞的低序位。

警告

請勿將此列舉中的值用於合併位運算。 列舉中的值不互斥。

注意

這個列舉無法測試 CAPS LOCK 或 NUM LOCK 金鑰目前是否已啟用。 您可以使用下列其中一種技術來判斷是否已啟用這些金鑰:

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

  • 如需更精細的控制,請使用 user32.dll 中定義的 Windows API 函式 GetKeyStateGetAsyncKeyStateGetKeyboardState 來執行此動作。 如需呼叫原生函式的詳細資訊,請參閱 取用 Unmanaged DLL 函式

下表顯示兩個列舉值所代表的按鍵碼值,代表一般原始設備製造商 (OEM) 鍵和更特定的美國鍵盤關聯。

十六進位值 美國鍵盤 一般 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

警告

針對 .NET Framework 2.0,已新增成員 IMEAccept,取代前一個專案 IMEAceept,其拼字不正確。 舊版已保留以供回溯相容性,但未來版本的.NET Framework可能會刪除

適用於

另請參閱