ConsoleKey 枚举

定义

指定主机上的标准键。

public enum class ConsoleKey
public enum ConsoleKey
[System.Serializable]
public enum ConsoleKey
type ConsoleKey = 
[<System.Serializable>]
type ConsoleKey = 
Public Enum ConsoleKey
继承
ConsoleKey
属性

字段

名称 说明
None 0
Backspace 8

Backspace 键。

Tab 9

Tab 键。

Clear 12

CLEAR 键。

Enter 13

ENTER 键。

Pause 19

Pause 键。

Escape 27

ESC (ESCAPE) 键。

Spacebar 32

空格键。

PageUp 33

Page Up 键。

PageDown 34

Page Down 键。

End 35

End 键。

Home 36

Home 键。

LeftArrow 37

向左键。

UpArrow 38

向上键。

RightArrow 39

向右键。

DownArrow 40

向下键。

Select 41

SELECT 键。

Print 42

PRINT 键。

Execute 43

EXECUTE 键。

PrintScreen 44

打印屏幕键。

Insert 45

INS (INSERT) 键。

Delete 46

DEL (DELETE) 键。

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

LeftWindows 91

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

RightWindows 92

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

Applications 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 键。

BrowserBack 166

浏览器后退键。

BrowserForward 167

浏览器转发键。

BrowserRefresh 168

浏览器刷新密钥。

BrowserStop 169

浏览器停止键。

BrowserSearch 170

浏览器搜索键。

BrowserFavorites 171

浏览器收藏夹密钥。

BrowserHome 172

浏览器主页键。

VolumeMute 173

音量静音键(Microsoft自然键盘)。

VolumeDown 174

音量向下键(Microsoft自然键盘)。

VolumeUp 175

音量向上键(Microsoft自然键盘)。

MediaNext 176

媒体下一跟踪密钥。

MediaPrevious 177

媒体上一曲目键。

MediaStop 178

媒体停止键。

MediaPlay 179

媒体播放/暂停键。

LaunchMail 180

“开始邮件”键(Microsoft自然键盘)。

LaunchMediaSelect 181

选择媒体键(Microsoft自然键盘)。

LaunchApp1 182

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

LaunchApp2 183

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

Oem1 186

OEM 1 键(特定于 OEM)。

OemPlus 187

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

OemComma 188

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

OemMinus 189

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

OemPeriod 190

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

Oem2 191

OEM 2 密钥(特定于 OEM)。

Oem3 192

OEM 3 键(特定于 OEM)。

Oem4 219

OEM 4 键(特定于 OEM)。

Oem5 220

OEM 5(特定于 OEM)。

Oem6 221

OEM 6 密钥(特定于 OEM)。

Oem7 222

OEM 7 密钥(特定于 OEM)。

Oem8 223

OEM 8 密钥(特定于 OEM)。

Oem102 226

OEM 102 密钥(特定于 OEM)。

Process 229

IME PROCESS 键。

Packet 231

PACKET 密钥(用于传递带击键的 Unicode 字符)。

Attention 246

ATTN 键。

CrSel 247

CRSEL (CURSOR SELECT) 键。

ExSel 248

EXSEL (EXTEND SELECTION) 键。

EraseEndOfFile 249

ERASE EOF 键。

Play 250

PLAY 键。

Zoom 251

ZOOM 键。

NoName 252

为将来使用保留的常量。

Pa1 253

PA1 键。

OemClear 254

CLEAR 键(特定于 OEM)。

示例

以下示例使用 ConsoleKey 枚举向用户指示用户按下的键。

using System;
using System.Text;

public class ConsoleKeyExample
{
   public static void Main()
   {
      ConsoleKeyInfo input;
      do {
         Console.WriteLine("Press a key, together with Alt, Ctrl, or Shift.");
         Console.WriteLine("Press Esc to exit.");
         input = Console.ReadKey(true);

         StringBuilder output = new StringBuilder(
                       String.Format("You pressed {0}", input.Key.ToString()));
         bool modifiers = false;

         if (input.Modifiers.HasFlag(ConsoleModifiers.Alt)) {
            output.Append(", together with " + ConsoleModifiers.Alt.ToString());
            modifiers = true;
         }
         if (input.Modifiers.HasFlag(ConsoleModifiers.Control))
         {
            if (modifiers) {
               output.Append(" and ");
            }
            else {
               output.Append(", together with ");
               modifiers = true;
            }
            output.Append(ConsoleModifiers.Control.ToString());
         }
         if (input.Modifiers.HasFlag(ConsoleModifiers.Shift))
         {
            if (modifiers) {
               output.Append(" and ");
            }
            else {
               output.Append(", together with ");
               modifiers = true;
            }
            output.Append(ConsoleModifiers.Shift.ToString());
         }
         output.Append(".");
         Console.WriteLine(output.ToString());
         Console.WriteLine();
      } while (input.Key != ConsoleKey.Escape);
   }
}
// The output from a sample console session might appear as follows:
//       Press a key, together with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed D.
//
//       Press a key, together with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed X, together with Shift.
//
//       Press a key, together with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed L, together with Control and Shift.
//
//       Press a key, together with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed P, together with Alt and Control and Shift.
//
//       Press a key, together with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed Escape.
open System
open System.Text

let mutable input = Unchecked.defaultof<ConsoleKeyInfo>

while input.Key <> ConsoleKey.Escape do
    printfn "Press a key, together with Alt, Ctrl, or Shift."
    printfn "Press Esc to exit."
    input <- Console.ReadKey true

    let output = StringBuilder $"You pressed {input.Key}"
    let mutable modifiers = false

    if input.Modifiers &&& ConsoleModifiers.Alt = ConsoleModifiers.Alt then
        output.Append ", together with {ConsoleModifiers.Alt}" |> ignore
        modifiers <- true

    if input.Modifiers &&& ConsoleModifiers.Control = ConsoleModifiers.Control then
        if modifiers then
            output.Append " and " |> ignore
    else
        output.Append ", together with " |> ignore
        modifiers <- true
        output.Append(string ConsoleModifiers.Control) |> ignore

    if input.Modifiers &&& ConsoleModifiers.Shift = ConsoleModifiers.Shift then
        if modifiers then
            output.Append " and " |> ignore
        else
            output.Append ", together with " |> ignore
            modifiers <- true
        output.Append(string ConsoleModifiers.Shift) |> ignore
    output.Append "." |> ignore
    
    printfn $"{output}\n"


// The output from a sample console session might appear as follows:
//       Press a key, along with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed D.
//
//       Press a key, along with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed X, along with Shift.
//
//       Press a key, along with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed L, along with Control and Shift.
//
//       Press a key, along with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed P, along with Alt and Control and Shift.
//
//       Press a key, along with Alt, Ctrl, or Shift.
//       Press Esc to exit.
//       You pressed Escape.
Imports System.Text

Module ConsoleKeyExample

   Public Sub Main()
      Dim input As ConsoleKeyInfo
      Do 
         Console.WriteLine("Press a key, together with Alt, Ctrl, or Shift.")
         Console.WriteLine("Press Esc to exit.")
         input = Console.ReadKey(True)

         Dim output As New StringBuilder(String.Format("You pressed {0}", input.Key.ToString()))
         Dim modifiers As Boolean

         If (input.Modifiers And ConsoleModifiers.Alt) = ConsoleModifiers.Alt Then
            output.Append(", together with " + ConsoleModifiers.Alt.ToString())
            modifiers = True
         End If
         If (input.Modifiers And ConsoleModifiers.Control) = ConsoleModifiers.Control Then
            If modifiers Then
               output.Append(" and ")
            Else
               output.Append(", together with ")
               modifiers = True
            End If
            output.Append(ConsoleModifiers.Control.ToString)
         End If
         If (input.Modifiers And ConsoleModifiers.Shift) = ConsoleModifiers.Shift Then
            If modifiers Then
               output.Append(" and ")
            Else
               output.Append(", together with ")
            End If
            output.Append(ConsoleModifiers.Shift.ToString)
         End If
         output.Append(".")                  
         Console.WriteLine(output.ToString())
         Console.WriteLine()
      Loop While input.Key <> ConsoleKey.Escape        
   End Sub
End Module
' The output from a sample console session might appear as follows:
'       Press a key, along with Alt, Ctrl, or Shift.
'       Press Esc to exit.
'       You pressed D.
'       
'       Press a key, along with Alt, Ctrl, or Shift.
'       Press Esc to exit.
'       You pressed X, along with Shift.
'       
'       Press a key, along with Alt, Ctrl, or Shift.
'       Press Esc to exit.
'       You pressed L, along with Control and Shift.
'       
'       Press a key, along with Alt, Ctrl, or Shift.
'       Press Esc to exit.
'       You pressed P, along with Alt and Control and Shift.
'       
'       Press a key, along with Alt, Ctrl, or Shift.
'       Press Esc to exit.
'       You pressed Escape.

注解

ConsoleKey枚举通常用于 nfo 结构中,该方法返回Console.ReadKey该结构以指示主机上的哪个键已按下。

适用于