Keys 列舉

定義

指定按鍵碼 (Key Code) 和修飾詞 (Modifier)。Specifies key codes and modifiers.

此列舉有 FlagsAttribute 屬性,因此其成員值可進行位元組合。

public enum class 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)]
public enum Keys
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.KeysConverter))>]
[<System.Flags>]
type Keys = 
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.KeysConverter))>]
[<System.Flags>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Keys = 
Public Enum Keys
繼承
屬性

欄位

A 65

A 鍵。The A key.

Add 107

加號鍵。The add key.

Alt 262144

ALT 輔助按鍵 (Modifier Key)。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

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

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

輸入法最終模式鍵。The IME final mode key.

G 71

G 鍵。The G key.

H 72

H 鍵。The H key.

HanguelMode 21

輸入法 Hanguel 模式鍵。The IME Hanguel mode key. (為相容性而維持;使用 HangulMode)(maintained for compatibility; use HangulMode)

HangulMode 21

輸入法 Hangul 模式鍵。The IME Hangul mode key.

HanjaMode 25

輸入法 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

輸入法接受鍵,取代 IMEAceeptThe IME accept key, replaces IMEAceept.

IMEAceept 30

輸入法接受鍵。The IME accept key. 已經過時,請改用 IMEAcceptObsolete, use IMEAccept instead.

IMEConvert 28

輸入法轉換鍵。The IME convert key.

IMEModeChange 31

輸入法模式變更鍵。The IME mode change key.

IMENonconvert 29

輸入法不轉換鍵。The IME nonconvert key.

Insert 45

INS 鍵。The INS key.

J 74

J 鍵。The J key.

JunjaMode 23

輸入法 Junja 模式鍵。The IME Junja mode key.

K 75

K 鍵。The K key.

KanaMode 21

輸入法 Kana 模式鍵。The IME Kana mode key.

KanjiMode 25

輸入法 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

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

PLAY 鍵。The PLAY key.

Print 42

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

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

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

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

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

  • 如需更精細的控制,請使用 Windows API 函式 GetKeyStateGetAsyncKeyStateGetKeyboardState 在 user32.dll 中定義的函式,以執行這項工作。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) 金鑰和更特定的美國鍵盤關聯。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,已加入的成員 IMEAccept 會取代先前的專案 IMEAceept,其拼寫錯誤。For the .NET Framework 2.0, a member IMEAccept was added that supersedes the previous entry, IMEAceept, which was spelled incorrectly. 較舊的版本已保留以提供回溯相容性,但可能會在未來的版本中刪除 .NET FrameworkThe older version has been retained for backward compatibility, but it may be deleted in future versions of the .NET Framework

適用於

另請參閱