Console::ReadKey Method (Boolean)


Obtains the next character or function key pressed by the user. The pressed key is optionally displayed in the console window.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

[HostProtectionAttribute(SecurityAction::LinkDemand, UI = true)]
static ConsoleKeyInfo ReadKey(
	bool intercept


Type: System::Boolean

Determines whether to display the pressed key in the console window. true to not display the pressed key; otherwise, false.

Return Value

Type: System::ConsoleKeyInfo

An 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.

Exception Condition

The In property is redirected from some stream other than the console.

The ReadKey method waits, that is, blocks on the thread issuing the ReadKey method, until a character or function key is pressed. A character or function key can be pressed in combination with one or more Alt, Ctrl, or Shift modifier keys. However, pressing a modifier key by itself will not cause the ReadKey method to return.

If the intercept parameter is true, the pressed key is intercepted and not displayed in the console window; otherwise, the pressed key is displayed.

Depending on your application, you might want to use the ReadKey method in conjunction with the KeyAvailable property.

The ReadKey method reads from the keyboard even if the standard input is redirected to a file with the SetIn method.

One of the most common uses of the ReadKey method is to halt program execution until the user presses a key and the app either terminates or displays an additional window of information. The following example uses the ReadKey(Boolean) method to wait for the user to press the Enter key before terminating the app. Note that, if the user presses any other key, it is not echoed to the console.

No code example is currently available or this language may not be supported.

The following example uses the ReadKey(Boolean) method to display information about the key pressed by a user without echoing that key to the console.

using namespace System;

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(true);
      Console::Write("You pressed ");
      if ((cki.Modifiers & ConsoleModifiers::Alt) != ConsoleModifiers()) Console::Write("ALT+");
      if ((cki.Modifiers & ConsoleModifiers::Shift) != ConsoleModifiers()) Console::Write("SHIFT+");
      if ((cki.Modifiers & ConsoleModifiers::Control) != ConsoleModifiers()) Console::Write("CTL+");
      Console::WriteLine("{0} (character '{1}')", cki.Key, cki.KeyChar);
   } while (cki.Key != ConsoleKey::Escape);
// This example displays output similar to the following:
//       Press any combination of CTL, ALT, and SHIFT, and a console key.
//       Press the Escape (Esc) key to quit:
//       You pressed CTL+A (character '☺')
//       You pressed C (character 'c')
//       You pressed CTL+C (character '♥')
//       You pressed K (character 'k')
//       You pressed ALT+I (character 'i')
//       You pressed ALT+U (character 'u')
//       You pressed ALT+SHIFT+H (character 'H')
//       You pressed Escape (character '←')

.NET Framework
Available since 2.0
Return to top