C# Console ReadKey()
Description
Console ReadKey()
obtains the next character or function
key pressed by the user. The pressed key is displayed in the console window.
Syntax
Console.ReadKey()
has the following syntax.
[HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)]
public static ConsoleKeyInfo ReadKey()
Returns
Console.ReadKey()
method returns A ConsoleKeyInfo object that describes the ConsoleKey constant and Unicode
character, if any, that correspond to the pressed console key. 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.
Example
The following example demonstrates the parameterless ReadKey method.
// w w w .ja va 2 s . c o m
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();
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);
}
}