ConsoleKeyInfo 结构

定义

描述按下的控制台键,包括控制台键表示的字符以及 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
继承
ConsoleKeyInfo
属性

示例

下面的示例演示如何在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.

对象描述与按下的控制台键对应的常量和Unicode字符(如果有)。ConsoleKey ConsoleKeyInfoThe ConsoleKeyInfo object describes the ConsoleKey constant and Unicode character, if any, that correspond to the pressed console key. 对象还在ConsoleModifiers值的按位组合中描述了一个或多个 SHIFT、ALT 和 CTRL 修改键是否同时按了控制台键。 ConsoleKeyInfoThe 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 结构的新实例。Initializes a new instance of the ConsoleKeyInfo structure using the specified character, console key, and modifier keys.

属性

Key

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

KeyChar

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

Modifiers

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

方法

Equals(ConsoleKeyInfo)

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

Equals(Object)

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

GetHashCode()

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

操作员

Equality(ConsoleKeyInfo, ConsoleKeyInfo)

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

Inequality(ConsoleKeyInfo, ConsoleKeyInfo)

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

适用于

另请参阅