ConsoleKeyInfo ConsoleKeyInfo ConsoleKeyInfo ConsoleKeyInfo Struct

定义

描述按下的控制台键,包括控制台键表示的字符以及 Shift、Alt 和 Ctrl 修改键的状态。Describes the console key that was pressed, including the character represented by the console key and the state of the SHIFT, ALT, and CTRL modifier keys.

public value class ConsoleKeyInfo
[System.Serializable]
public struct ConsoleKeyInfo
type ConsoleKeyInfo = struct
Public Structure ConsoleKeyInfo
继承
ConsoleKeyInfoConsoleKeyInfoConsoleKeyInfoConsoleKeyInfo
属性

示例

下面的示例演示了如何使用ConsoleKeyInfo在读取操作中的对象。The following example demonstrates using a ConsoleKeyInfo object in a read operation.

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 
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类型不应由用户来创建。The ConsoleKeyInfo type is not intended to be created by users. 相反,它将返回到调用的响应中的用户Console.ReadKey方法。Instead, it is returned to the user in response to calling the Console.ReadKey method.

ConsoleKeyInfo对象描述ConsoleKey常量和 Unicode 字符,如果有,对应于按下的控制台键。The ConsoleKeyInfo object describes the ConsoleKey constant and Unicode character, if any, that correspond to the pressed console key. ConsoleKeyInfo对象还中描述的按位组合ConsoleModifiers值,一个或多个 SHIFT、 ALT 或 CTRL 修改键已按使用的控制台键同时下。The ConsoleKeyInfo object also describes, in a bitwise combination of ConsoleModifiers values, whether one or more SHIFT, ALT, or CTRL modifier keys was pressed simultaneously with the console key.

构造函数

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

用指定的字符、控制台键和修改键初始化 ConsoleKeyInfo 结构的新实例。Initializes a new instance of the ConsoleKeyInfo structure using the specified character, console key, and modifier keys.

属性

Key Key Key Key

获取当前 ConsoleKeyInfo 对象表示的控制台键。Gets the console key represented by the current ConsoleKeyInfo object.

KeyChar KeyChar KeyChar KeyChar

获取当前 ConsoleKeyInfo 对象表示的 Unicode 字符。Gets the Unicode character represented by the current ConsoleKeyInfo object.

Modifiers Modifiers Modifiers Modifiers

获取 ConsoleModifiers 值的一个按位组合,指定与控制台键同时按下的一个或多个修改键。Gets a bitwise combination of ConsoleModifiers values that specifies one or more modifier keys pressed simultaneously with the console key.

方法

Equals(ConsoleKeyInfo) Equals(ConsoleKeyInfo) Equals(ConsoleKeyInfo) Equals(ConsoleKeyInfo)

获取一个值,该值指示指定的 ConsoleKeyInfo 对象是否等于当前 ConsoleKeyInfo 对象。Gets a value indicating whether the specified ConsoleKeyInfo object is equal to the current ConsoleKeyInfo object.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

获取一个值,该值指示指定的对象是否等于当前的 ConsoleKeyInfo 对象。Gets a value indicating whether the specified object is equal to the current ConsoleKeyInfo object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回当前 ConsoleKeyInfo 对象的哈希代码。Returns the hash code for the current ConsoleKeyInfo object.

操作员

Equality(ConsoleKeyInfo, ConsoleKeyInfo) Equality(ConsoleKeyInfo, ConsoleKeyInfo) Equality(ConsoleKeyInfo, ConsoleKeyInfo) Equality(ConsoleKeyInfo, ConsoleKeyInfo)

指示指定的 ConsoleKeyInfo 对象是否相等。Indicates whether the specified ConsoleKeyInfo objects are equal.

Inequality(ConsoleKeyInfo, ConsoleKeyInfo) Inequality(ConsoleKeyInfo, ConsoleKeyInfo) Inequality(ConsoleKeyInfo, ConsoleKeyInfo) Inequality(ConsoleKeyInfo, ConsoleKeyInfo)

指示指定的 ConsoleKeyInfo 对象是否不相等。Indicates whether the specified ConsoleKeyInfo objects are not equal.

适用于

另请参阅