BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 2.0+

Console Application Input

Console applications are ideal for small tools and utilities where a graphical user interface is unnecessary. With console applications primary input is via the keyboard, either by requesting lines of text or detecting individual key presses.

Console Class

The Console class contains a set of static methods and properties that allow you to interact with the user in a console application. The members include two important methods for accepting direct input from the user. These are the subject of this article.

Reading a Line of Text

It's common to request a line of text from the user. This might represent some information to be processed or a command that your program understands and can execute. To receive this type of input, you can use the ReadLine method. ReadLine creates a buffer that can receive up to 254 characters. As the user types, the buffer is updated. When the user presses Enter, the buffer's content is returned as a string. At this point the program continues from the command following the ReadLine.

We can demonstrate this very easily by adding the following code to the Main method of a console application. The code uses WriteLine to prompt the user to enter some text. The entered information is obtained using ReadLine and stored in the string variable named, "input". To show that the text was correctly received it is echoed back to the user. The final Console.ReadLine waits for Enter to be pressed before the program exits.

Console.WriteLine("Enter a line of text, then press Enter");
string input = Console.ReadLine();
Console.WriteLine("You typed: {0}", input);
Console.ReadLine();

Reading a Single Character

Sometimes you'll only want the user to provide a single key press. For example, you might ask a Yes/No question and wait for the user to press either Y or N in response. For this type of operation you can use the ReadKey method. ReadKey waits for the user to press a key, or a key combination including one or more of the control, alt and shift keys. The user does not need to press Enter afterwards for the method to return.

ReadKey returns a value of the ConsoleKeyInfo structure. This structure includes several properties that permit you to understand which key was pressed. For simple operations you can check the KeyChar property. This returns the character that the single key or keyboard combination produces. If you need more control, you can use the Key and Modifiers properties.

Key returns a ConsoleKey value. ConsoleKey is an enumeration containing constants for all of the keys on a standard keyboard that produce input normally. This includes all letters, numbers and symbols and some control keys such as delete, backspace and return. It doesn't include non-input keys such as control, alt and shift. However, you can check the state of these with the Modifiers property.

Modifiers returns a ConsoleModifiers value. This is another enumeration with values for Control, Alt and Shift. It is a bit field enumeration, so you can detect which of the three keys, if any, was used.

The following example requests a key press and displays the three ConsoleKeyInfo property values.

Console.WriteLine("Press any key");
ConsoleKeyInfo input = Console.ReadKey();
Console.WriteLine("Character pressed: {0}", input.KeyChar);
Console.WriteLine("Modifiers Used: {0}", input.Modifiers);
Console.WriteLine("Key Used: {0}", input.Key);
Console.ReadLine();

When you use this version of the ReadKey method, the key pressed is outputted to the console. For example, if you run the program and press Shift-Q, the output will be as shown below. Note the additional Q at the start of the second line of text.

Press any key
QCharacter pressed: Q
Modifiers Used: Shift
Key Used: Q

If you want to hide the input you can, using an overloaded version of ReadKey. This version requires a Boolean parameter. If true, the key press will not be echoed to the console. If false, the method uses the default behaviour and the key press is shown.

To demonstrate, modify the code as follows and try running the program again. The comment in the sample code shows the output when you press Shift-Q. This time the additional Q character is absent.

Console.WriteLine("Press any key");
ConsoleKeyInfo input = Console.ReadKey(true);
Console.WriteLine("Character pressed: {0}", input.KeyChar);
Console.WriteLine("Modifiers Used: {0}", input.Modifiers);
Console.WriteLine("Key Used: {0}", input.Key);
Console.ReadLine();

/* OUTPUT

Press any key
Character pressed: Q
Modifiers Used: Shift
Key Used: Q

*/
7 August 2013