#972 – Reading Keystrokes from the Console

The Console.Read method reads input from a console window one character at a time but does not begin reading characters until after the user has pressed Enter.

The Console.ReadKey method, on the other hand, reads a key that the user presses immediately, without waiting for the user to press Enter. The ReadKey method returns a value whose type is ConsoleKeyInfo, which contains fields that tell you which key was pressed:

Key field – ConsoleKey (enumeration) value indicating the exact key that was pressed

KeyChar field – Unicode character (of type char) for the key that was pressed

In the example below, we read two keypresses before displaying what was entered. Notice that the output is shown after we enter two characters, but before we’ve pressed the Enter key.