Home
Map
Console.ReadKey ExamplesUse the Console.ReadKey method to handle keys as they are pressed in an interactive way.
C#
This page was last reviewed on Nov 7, 2023.
Console.ReadKey. This method helps create interactive console programs. It does not require the user to press enter before returning. It can read modifiers such as control.
ConsoleKeyInfo. When ReadKey is called, a ConsoleKeyInfo struct is returned. We test this struct to enable more powerful text programs.
Console.WriteLine
Example program. Here we use Console.ReadKey with no arguments. The return value of the Console.ReadKey method is an instance of the ConsoleKeyInfo struct.
public, private
Info ReadKey returns once a key is pressed. We call instance properties on the ConsoleKeyInfo to determine the input.
Also ConsoleKeyInfo is a struct. ReadKey() returns a new ConsoleKeyInfo instance on each call.
struct
Detail When you call the Key property accessor on the ConsoleKeyInfo, you are reading the values that were already set in the struct.
And The Modifiers property returns a value of type ConsoleModifiers, which is an enumerated constant.
Property
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(); } }
(Keys were pressed) ... Press escape, a, then control X ?You pressed escape! aYou pressed a ?You pressed control X
Game example. We next emphasize one of the uses for the ReadKey method. With this method, you can create fully interactive console programs in the C# language.
And This allows you to implement simple games, such as maze programs and adventure games.
Tip You could use a maze program with a pathfinding algorithm to learn more about graph algorithms and other searching routines.
Maze
Info In the old days of computing, many adventure games were implemented in console form.
Here The lowercase "wasd" keys control the cursor as it moves around a 5 by 5 board.
switch
using System; class Program { static char[,] _board = new char[5, 5]; static void Main() { int positionX = 0; int positionY = 0; while (true) { // At current position, write special char and update board. _board[positionX, positionY] = '@'; for (int i = 0; i < 5; i++) { for (int z = 0; z < 5; z++) { Console.Write(_board[z, i] == char.MinValue ? "." : _board[z, i]); } Console.WriteLine(); // End line. } // Handle directional keys with ReadKey. Console.Write(">"); var info = Console.ReadKey(); Console.WriteLine(); switch (info.KeyChar) { case 'w': positionY--; break; case 'a': positionX--; break; case 's': positionY++; break; case 'd': positionX++; break; } } } }
@.... ..... ..... ..... ..... >d @@... ..... ..... ..... ..... >d @@@.. ..... ..... ..... ..... >s @@@.. ..@.. ..... ..... ..... >s @@@.. ..@.. ..@.. ..... ..... >s @@@.. ..@.. ..@.. ..@.. ..... >d @@@.. ..@.. ..@.. ..@@. ..... >d @@@.. ..@.. ..@.. ..@@@ ..... >w @@@.. ..@.. ..@.@ ..@@@ ..... >
Summary. Console.ReadKey can read keys from the console window and immediately return the value. You can use the ConsoleKeyInfo struct to then access the values read in your C# code.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 7, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.