共用方式為


ConsoleKeyInfo 結構

定義

說明按下的主控台按鍵,包括主控台按鍵表示的字元,以及 SHIFT、ALT 和 CTRL 輔助按鍵 (Modifier Key) 的狀態。

public value class ConsoleKeyInfo : IEquatable<ConsoleKeyInfo>
public value class ConsoleKeyInfo
public readonly struct ConsoleKeyInfo : IEquatable<ConsoleKeyInfo>
public struct ConsoleKeyInfo
public readonly struct ConsoleKeyInfo
[System.Serializable]
public struct ConsoleKeyInfo
type ConsoleKeyInfo = struct
[<System.Serializable>]
type ConsoleKeyInfo = struct
Public Structure ConsoleKeyInfo
Implements IEquatable(Of ConsoleKeyInfo)
Public Structure ConsoleKeyInfo
繼承
ConsoleKeyInfo
屬性
實作

範例

下列範例示範如何在 ConsoleKeyInfo 讀取作業中使用 物件。

using namespace System;

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) != ConsoleModifiers()) Console::Write("ALT+");
      if((cki.Modifiers & ConsoleModifiers::Shift) != ConsoleModifiers()) Console::Write("SHIFT+");
      if((cki.Modifiers & ConsoleModifiers::Control) != ConsoleModifiers()) 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 }
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
open System

// Prevent example from ending if CTL+C is pressed.
Console.TreatControlCAsInput <- true

printfn "Press any combination of CTL, ALT, and SHIFT, and a console key."
printfn "Press the Escape (Esc) key to quit: \n"

let mutable cki = Unchecked.defaultof<ConsoleKeyInfo>

while cki.Key <> ConsoleKey.Escape do
    cki <- Console.ReadKey()
    printf " --- You pressed "
    if int (cki.Modifiers &&& ConsoleModifiers.Alt) <> 0 then printf "ALT+"
    if int (cki.Modifiers &&& ConsoleModifiers.Shift) <> 0 then printf "SHIFT+"
    if int (cki.Modifiers &&& ConsoleModifiers.Control) <> 0 then printf "CTL+"
    printfn $"{cki.Key}"


// 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
Class Example
   Public Shared Sub Main()
      Dim cki As ConsoleKeyInfo
      ' 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: " + vbCrLf)
      Do
         cki = Console.ReadKey()
         Console.Write(" --- You pressed ")
         If (cki.Modifiers And ConsoleModifiers.Alt) <> 0 Then Console.Write("ALT+")
         If (cki.Modifiers And ConsoleModifiers.Shift) <> 0 Then Console.Write("SHIFT+")
         If (cki.Modifiers And ConsoleModifiers.Control) <> 0 Then Console.Write("CTL+")
         Console.WriteLine(cki.Key.ToString)
      Loop While cki.Key <> ConsoleKey.Escape
   End Sub 
End Class 
' 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

備註

ConsoleKeyInfo 類型不適合由使用者建立。 相反地,它會傳回給使用者,以回應呼叫 Console.ReadKey 方法。

物件 ConsoleKeyInfoConsoleKey 描述常數和 Unicode 字元,如果有的話,對應到按下的主控台鍵。 物件 ConsoleKeyInfo 也會在值的位元組合 ConsoleModifiers 中描述一或多個 SHIFT、ALT 或 CTRL 修飾詞鍵是否與主控台鍵同步選取。

建構函式

ConsoleKeyInfo(Char, ConsoleKey, Boolean, Boolean, Boolean)

使用指定的字元、主控台按鍵和輔助按鍵來初始化 ConsoleKeyInfo 結構的新執行個體。

屬性

Key

取得目前的 ConsoleKeyInfo 物件表示的主控台按鍵。

KeyChar

取得目前的 ConsoleKeyInfo 物件表示的 Unicode 字元。

Modifiers

取得 ConsoleModifiers 值的位元 (Bitwise) 組合,指定一或多個輔助按鍵與主控台按鍵同時按下。

方法

Equals(ConsoleKeyInfo)

取得值,指出指定的 ConsoleKeyInfo 物件是否和目前的 ConsoleKeyInfo 物件相等。

Equals(Object)

取得值,指出指定的物件是否等於目前的 ConsoleKeyInfo 物件。

GetHashCode()

傳回目前 ConsoleKeyInfo 物件的雜湊碼。

運算子

Equality(ConsoleKeyInfo, ConsoleKeyInfo)

表示指定的 ConsoleKeyInfo 物件是否相等。

Inequality(ConsoleKeyInfo, ConsoleKeyInfo)

表示指定的 ConsoleKeyInfo 物件是否不相等。

適用於

另請參閱