Console.ReadKey Method (Boolean)

Updated: December 2009

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)]
public static ConsoleKeyInfo ReadKey(
	bool intercept
)

Parameters

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
A ConsoleKeyInfo 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.

ExceptionCondition
InvalidOperationException

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

NoteNote:

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: UI. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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.

The following example demonstrates the ReadKey method that takes a Boolean parameter.

using System;

class Example 
{
   public static 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) != 0) Console.Write("ALT+");
         if ((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
         if ((cki.Modifiers & ConsoleModifiers.Control) != 0) 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 '←')

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Date

History

Reason

December 2009

Modified the example.

Customer feedback.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft