使用英语阅读

通过


ConsoleModifiers 枚举

定义

表示键盘上的 Shift、Alt 和 Ctrl 修改键。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum ConsoleModifiers
C#
[System.Flags]
[System.Serializable]
public enum ConsoleModifiers
继承
ConsoleModifiers
属性

字段

名称 说明
Alt 1

左侧或右侧的 Alt 修改键。

Control 4

左侧或右侧的 Ctrl 修改键。

Shift 2

左侧或右侧的 Shift 修改键。

示例

下面的代码示例读取一个键,并确定是按下了一个或多个修饰键。

C#
using System;

class Example
{
   public static void Main()
   {
      ConsoleKeyInfo cki;
      // Prevent example from ending if CTL+C is pressed.
      Console.TreatControlCAsInput = true;

      Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
      Console.WriteLine("Press the Escape (Esc) key to quit: \n");
      do
      {
         cki = Console.ReadKey();
         Console.Write(" --- You pressed ");
         if((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
         if((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
         if((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
         Console.WriteLine(cki.Key.ToString());
       } while (cki.Key != ConsoleKey.Escape);
    }
}
// This example displays output similar to the following:
//       Press any combination of CTL, ALT, and SHIFT, and a console key.
//       Press the Escape (Esc) key to quit:
//
//       a --- You pressed A
//       k --- You pressed ALT+K
//       ► --- You pressed CTL+P
//         --- You pressed RightArrow
//       R --- You pressed SHIFT+R
//                --- You pressed CTL+I
//       j --- You pressed ALT+J
//       O --- You pressed SHIFT+O
//       § --- You pressed CTL+U

注解

不区分左键或右 Shift 键、ALT 键和 CTRL 键。

枚举 ConsoleModifiers 与 类型一起使用 ConsoleKeyInfo

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

另请参阅