ConsoleKeyInfo Struktur

Definisi

Menjelaskan kunci konsol yang ditekan, termasuk karakter yang diwakili oleh kunci konsol dan status tombol pengubah SHIFT, ALT, dan CTRL.

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
Warisan
ConsoleKeyInfo
Atribut
Penerapan

Contoh

Contoh berikut menunjukkan penggunaan ConsoleKeyInfo objek dalam operasi baca.

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

Keterangan

Jenis ConsoleKeyInfo ini tidak dimaksudkan untuk dibuat oleh pengguna. Sebaliknya, itu dikembalikan kepada pengguna sebagai respons untuk memanggil Console.ReadKey metode .

Objek ConsoleKeyInfo menjelaskan konstanta ConsoleKey dan karakter Unicode, jika ada, yang sesuai dengan tombol konsol yang ditekan. Objek ini ConsoleKeyInfo juga menjelaskan, dalam kombinasi ConsoleModifiers nilai bitwise, apakah satu atau beberapa tombol pengubah SHIFT, ALT, atau CTRL ditekan secara bersamaan dengan kunci konsol.

Konstruktor

Nama Deskripsi
ConsoleKeyInfo(Char, ConsoleKey, Boolean, Boolean, Boolean)

Menginisialisasi instans ConsoleKeyInfo baru struktur menggunakan karakter, kunci konsol, dan kunci pengubah yang ditentukan.

Properti

Nama Deskripsi
Key

Mendapatkan kunci konsol yang diwakili oleh objek saat ini ConsoleKeyInfo .

KeyChar

Mendapatkan karakter Unicode yang diwakili oleh objek saat ini ConsoleKeyInfo .

Modifiers

Mendapatkan kombinasi ConsoleModifiers nilai bitwise yang menentukan satu atau beberapa tombol pengubah yang ditekan secara bersamaan dengan kunci konsol.

Metode

Nama Deskripsi
Equals(ConsoleKeyInfo)

Mendapatkan nilai yang menunjukkan apakah objek yang ditentukan ConsoleKeyInfo sama dengan objek saat ini ConsoleKeyInfo .

Equals(Object)

Mendapatkan nilai yang menunjukkan apakah objek yang ditentukan sama dengan objek saat ini ConsoleKeyInfo .

GetHashCode()

Mengembalikan kode hash untuk objek saat ini ConsoleKeyInfo .

Operator

Nama Deskripsi
Equality(ConsoleKeyInfo, ConsoleKeyInfo)

Menunjukkan apakah objek yang ditentukan ConsoleKeyInfo sama.

Inequality(ConsoleKeyInfo, ConsoleKeyInfo)

Menunjukkan apakah objek yang ditentukan ConsoleKeyInfo tidak sama.

Berlaku untuk

Lihat juga