ConsoleKeyInfo 结构
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
描述按下的控制台键,包括控制台键表示的字符以及 Shift、Alt 和 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
- 继承
- 属性
- 实现
示例
以下示例演示如何 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 。
该 ConsoleKeyInfo 对象描述 ConsoleKey 与按下的控制台键对应的常量和 Unicode 字符(如果有)。 该 ConsoleKeyInfo 对象还以按位方式描述值的组合 ConsoleModifiers ,无论一个或多个 SHIFT、ALT 或 Ctrl 修饰键与控制台键同时按下。
构造函数
ConsoleKeyInfo(Char, ConsoleKey, Boolean, Boolean, Boolean) |
用指定的字符、控制台键和修改键初始化 ConsoleKeyInfo 结构的新实例。 |
属性
Key |
获取当前 ConsoleKeyInfo 对象表示的控制台键。 |
KeyChar |
获取当前 ConsoleKeyInfo 对象表示的 Unicode 字符。 |
Modifiers |
获取 ConsoleModifiers 值的一个按位组合,指定与控制台键同时按下的一个或多个修改键。 |
方法
Equals(ConsoleKeyInfo) |
获取一个值,该值指示指定的 ConsoleKeyInfo 对象是否等于当前 ConsoleKeyInfo 对象。 |
Equals(Object) |
获取一个值,该值指示指定的对象是否等于当前的 ConsoleKeyInfo 对象。 |
GetHashCode() |
返回当前 ConsoleKeyInfo 对象的哈希代码。 |
运算符
Equality(ConsoleKeyInfo, ConsoleKeyInfo) |
指示指定的 ConsoleKeyInfo 对象是否相等。 |
Inequality(ConsoleKeyInfo, ConsoleKeyInfo) |
指示指定的 ConsoleKeyInfo 对象是否不相等。 |