ConsoleKeyInfo Struct-datatyp

Definition

Beskriver den konsolnyckel som trycktes ned, inklusive tecknet som representeras av konsolnyckeln och tillståndet för skift-, ALT- och CTRL-modifierarnycklarna.

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
Arv
ConsoleKeyInfo
Attribut
Implementeringar

Exempel

I följande exempel visas hur du använder ett ConsoleKeyInfo objekt i en läsåtgärd.

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

Kommentarer

Typen ConsoleKeyInfo är inte avsedd att skapas av användare. I stället returneras den till användaren som svar på anropet av Console.ReadKey metoden.

Objektet ConsoleKeyInfo beskriver konstant- ConsoleKey och Unicode-tecknet, om det finns något, som motsvarar den pressade konsolnyckeln. Objektet ConsoleKeyInfo beskriver också, i en bitvis kombination av ConsoleModifiers värden, om en eller flera SKIFT-, ALT- eller CTRL-modifierarnycklar trycktes ned samtidigt med konsolnyckeln.

Konstruktorer

Name Description
ConsoleKeyInfo(Char, ConsoleKey, Boolean, Boolean, Boolean)

Initierar en ny instans av ConsoleKeyInfo strukturen med det angivna tecknet, konsolnyckeln och modifierarnycklarna.

Egenskaper

Name Description
Key

Hämtar konsolnyckeln som representeras av det aktuella ConsoleKeyInfo objektet.

KeyChar

Hämtar Unicode-tecknet som representeras av det aktuella ConsoleKeyInfo objektet.

Modifiers

Hämtar en bitvis kombination av ConsoleModifiers värden som anger en eller flera modifierarnycklar som trycks in samtidigt med konsolnyckeln.

Metoder

Name Description
Equals(ConsoleKeyInfo)

Hämtar ett värde som anger om det angivna ConsoleKeyInfo objektet är lika med det aktuella ConsoleKeyInfo objektet.

Equals(Object)

Hämtar ett värde som anger om det angivna objektet är lika med det aktuella ConsoleKeyInfo objektet.

GetHashCode()

Returnerar hash-koden för det aktuella ConsoleKeyInfo objektet.

Operatorer

Name Description
Equality(ConsoleKeyInfo, ConsoleKeyInfo)

Anger om de angivna ConsoleKeyInfo objekten är lika med.

Inequality(ConsoleKeyInfo, ConsoleKeyInfo)

Anger om de angivna ConsoleKeyInfo objekten inte är lika med.

Gäller för

Se även