C# ConsoleKeyInfo Key
Description
ConsoleKeyInfo Key
gets the console key represented
by the current ConsoleKeyInfo object.
Syntax
ConsoleKeyInfo.Key
has the following syntax.
public ConsoleKey Key { get; }
Example
The following example demonstrates using a Key property in a read operation.
//w ww. j av a 2s .c o m
using System;
class Example
{
public static void Main()
{
ConsoleKeyInfo cki;
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();
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);
}
}
The code above generates the following result.