C# Console.ReadKey Method

Console screenshot

You are interested in specifics of the Console.ReadKey method and how it can be used to create interactive console programs, such as those that used to be popular as applications. The Console.ReadKey method does not require the user to press enter before returning, and it can read modifiers and enable more powerful text programs.

Example

Note

In this program, we look at how you can use the Console.ReadKey public static method with no arguments. The return of the Console.ReadKey method is an instance of the ConsoleKeyInfo struct; you can declare a local variable of this type. Then, after ReadKey returns, the struct is assigned and you can call instance properties on the struct instance to determine what the input was.

This C# example program uses the Console.ReadKey method. It handles keys as they are pressed.

Program that uses Console.ReadKey [C#]

using System;

class Program
{
    static void Main()
    {
	Console.WriteLine("... Press escape, a, then control X");
	// Call ReadKey method and store result in local variable.
	// ... Then test the result for escape.
	ConsoleKeyInfo info = Console.ReadKey();
	if (info.Key == ConsoleKey.Escape)
	{
	    Console.WriteLine("You pressed escape!");
	}
	// Call ReadKey again and test for the letter a.
	info = Console.ReadKey();
	if (info.KeyChar == 'a')
	{
	    Console.WriteLine("You pressed a");
	}
	// Call ReadKey again and test for control-X.
	// ... This implements a shortcut sequence.
	info = Console.ReadKey();
	if (info.Key == ConsoleKey.X &&
	    info.Modifiers == ConsoleModifiers.Control)
	{
	    Console.WriteLine("You pressed control X");
	}
	Console.Read();
    }
}

Output
    (Keys were pressed)

... Press escape, a, then control X
?You pressed escape!
aYou pressed a
?You pressed control X

About ConsoleKeyInfo struct. When you hover your mouse over the local variable of type ConsoleKeyInfo, Visual Studio will tell you that it is a struct. This means it is a value type and that it is basically immutable; however, the local variable can be reassigned. The ReadKey method will return a new ConsoleKeyInfo instance each time it is called.

Using ReadKey method on ConsoleKeyInfo. The ReadKey method immediately causes the program execution to stop and wait for the input. However, unlike other Console methods, ReadKey will immediately return when a key has been pressed by the user. Modifier keys will not cause the immediate return, so control sequences are possible.

Property (Icon copyright Microsoft)

Using Key property on ConsoleKeyInfo. When you call the Key property accessor on the local ConsoleKeyInfo variable, you are reading the values that were already set in the struct. In this program, the escape key is detected. The enter key does not need to be pressed to cause the program code to resume execution.

Using Modifiers property on ConsoleKeyInfo. The Modifiers property returns a value of type ConsoleModifiers, which is an enumerated constant. You can call the Modifiers property and immediately test it against the ConsoleModifiers constants, such as ConsoleModifiers.Control.

Interactive console programs

Programming tip

Let's emphasize one of the uses for the ReadKey method. With this method, you can create fully interactive console programs in the C# language. This allows you to implement simple games, such as maze programs and adventure games. You could use a maze program with a pathfinding algorithm to learn more about graph algorithms and other searching routines. In the old days of computing, many adventure games were implemented in console form.

Pathfinding Algorithm

Summary

We checked out the Console.ReadKey method in the C# programming language and saw how it can be used to read keys from the console window and return the value immediately. You can use the ConsoleKeyInfo struct to then access the values read in your C# code. We noted the Key and Modifiers properties as well, and finally visited certain applications of interactive console programs.

Console Programs
.NET