ConsoleKey 枚举 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定控制台上的标准键。
public enum class ConsoleKeypublic enum ConsoleKey[System.Serializable]
public enum ConsoleKeytype ConsoleKey = [<System.Serializable>]
type ConsoleKey = Public Enum ConsoleKey- 继承
- 属性
字段
| 名称 | 值 | 说明 | 
|---|---|---|
| A | 65 | A 键。 | 
| Add | 107 | 加号键(数字键盘上的加号键)。 | 
| Applications | 93 | 应用程序键 (Microsoft Natural Keyboard)。 | 
| Attention | 246 | ATTN 键。 | 
| B | 66 | B 键。 | 
| Backspace | 8 | BACKSPACE 键。 | 
| BrowserBack | 166 | 浏览器后退键。 | 
| BrowserFavorites | 171 | 浏览器收藏键。 | 
| BrowserForward | 167 | 浏览器前进键。 | 
| BrowserHome | 172 | 浏览器主页键。 | 
| BrowserRefresh | 168 | 浏览器刷新键。 | 
| BrowserSearch | 170 | 浏览器搜索键。 | 
| BrowserStop | 169 | 浏览器停止键。 | 
| C | 67 | C 键。 | 
| Clear | 12 | CLEAR 键。 | 
| 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 (DELETE) 键。 | 
| Divide | 111 | 除号键(数字键盘上的除号键)。 | 
| DownArrow | 40 | DOWN ARROW 键。 | 
| E | 69 | E 键。 | 
| End | 35 | END 键。 | 
| Enter | 13 | ENTER 键。 | 
| EraseEndOfFile | 249 | ERASE EOF 键。 | 
| Escape | 27 | ESC (ESCAPE) 键。 | 
| 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 键。 | 
| G | 71 | G 键。 | 
| H | 72 | H 键。 | 
| Help | 47 | HELP 键。 | 
| Home | 36 | HOME 键。 | 
| I | 73 | I 键。 | 
| Insert | 45 | INS (INSERT) 键。 | 
| J | 74 | J 键。 | 
| K | 75 | K 键。 | 
| L | 76 | L 键。 | 
| LaunchApp1 | 182 | 启动应用程序 1 键 (Microsoft Natural Keyboard)。 | 
| LaunchApp2 | 183 | 启动应用程序 2 键 (Microsoft Natural Keyboard)。 | 
| LaunchMail | 180 | 启动邮件键 (Microsoft Natural Keyboard)。 | 
| LaunchMediaSelect | 181 | 选择媒体键 (Microsoft Natural Keyboard)。 | 
| LeftArrow | 37 | LEFT ARROW 键。 | 
| LeftWindows | 91 | 左 Windows 徽标键 (Microsoft Natural Keyboard)。 | 
| M | 77 | M 键。 | 
| MediaNext | 176 | 媒体下一曲目键。 | 
| MediaPlay | 179 | 媒体播放/暂停键。 | 
| MediaPrevious | 177 | 媒体上一曲目键。 | 
| MediaStop | 178 | 媒体停止键。 | 
| Multiply | 106 | 乘号键(数字键盘上的乘号键)。 | 
| N | 78 | N 键。 | 
| NoName | 252 | 留待将来使用的常数。 | 
| 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 键(特定于 OEM)。 | 
| Oem102 | 226 | OEM 102 键(特定于 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)。 | 
| OemClear | 254 | CLEAR 键(特定于 OEM)。 | 
| OemComma | 188 | 任何国家/地区键盘上的 OEM 逗号键。 | 
| OemMinus | 189 | 任何国家/地区键盘上的 OEM 减号键。 | 
| OemPeriod | 190 | 任何国家/地区键盘上的 OEM 句号键。 | 
| OemPlus | 187 | 任何国家/地区键盘上的 OEM 加号键。 | 
| P | 80 | P 键。 | 
| Pa1 | 253 | PA1 键。 | 
| Packet | 231 | PACKET 键(通过击键次数用于传递 Unicode 字符)。 | 
| PageDown | 34 | PAGE DOWN 键。 | 
| PageUp | 33 | PAGE UP 键。 | 
| Pause | 19 | PAUSE 键。 | 
| Play | 250 | 播放键。 | 
| 42 | PRINT 键。 | |
| PrintScreen | 44 | PRINT SCREEN 键。 | 
| Process | 229 | IME PROCESS 键。 | 
| Q | 81 | Q 键。 | 
| R | 82 | R 键。 | 
| RightArrow | 39 | RIGHT ARROW 键。 | 
| RightWindows | 92 | 右 Windows 徽标键 (Microsoft Natural Keyboard)。 | 
| S | 83 | S 键。 | 
| Select | 41 | SELECT 键。 | 
| Separator | 108 | 分隔符键。 | 
| Sleep | 95 | 计算机休眠键。 | 
| Spacebar | 32 | SPACEBAR 键。 | 
| Subtract | 109 | 减号键(数字键盘上的减号键)。 | 
| T | 84 | T 键。 | 
| Tab | 9 | TAB 键。 | 
| U | 85 | U 键。 | 
| UpArrow | 38 | UP ARROW 键。 | 
| V | 86 | V 键。 | 
| VolumeDown | 174 | 调低音量键 (Microsoft Natural Keyboard)。 | 
| VolumeMute | 173 | 静音键 (Microsoft Natural Keyboard)。 | 
| VolumeUp | 175 | 调高音量键 (Microsoft Natural Keyboard)。 | 
| W | 87 | W 键。 | 
| X | 88 | X 键。 | 
| Y | 89 | Y 键。 | 
| Z | 90 | Z 键。 | 
| Zoom | 251 | 缩放键。 | 
示例
以下示例使用 ConsoleKey 枚举向用户指示用户按下了哪个键。
using namespace System;
using namespace System::Text;
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 = gcnew StringBuilder(
                     String::Format("You pressed {0}", input.Key.ToString()));
      bool modifiers = false;
      if ((input.Modifiers & ConsoleModifiers::Alt) == ConsoleModifiers::Alt) {
         output->Append(", together with " + ConsoleModifiers::Alt.ToString());
         modifiers = true;
      }
      if ((input.Modifiers & ConsoleModifiers::Control) == ConsoleModifiers::Control)
      {
         if (modifiers) {
            output->Append(" and ");
         }   
         else {
            output->Append(", together with ");
            modifiers = true;
         }
         output->Append(ConsoleModifiers::Control.ToString());
      }
      if ((input.Modifiers & ConsoleModifiers::Shift) == 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, 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.
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 & ConsoleModifiers.Alt) == ConsoleModifiers.Alt) {
            output.Append(", together with " + ConsoleModifiers.Alt.ToString());
            modifiers = true;
         }
         if ((input.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control)
         {
            if (modifiers) {
               output.Append(" and ");
            }
            else {
               output.Append(", together with ");
               modifiers = true;
            }
            output.Append(ConsoleModifiers.Control.ToString());
         }
         if ((input.Modifiers & ConsoleModifiers.Shift) == 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, 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.
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该结构以指示已按下主机上的哪个键。