閱讀英文版本

分享方式:


ConsoleModifiers 列舉

定義

代表鍵盤上的 SHIFT、ALT 與 CTRL 等輔助按鍵。

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

C#
[System.Flags]
public enum ConsoleModifiers
C#
[System.Flags]
[System.Serializable]
public enum ConsoleModifiers
繼承
ConsoleModifiers
屬性

欄位

名稱 Description
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

另請參閱