Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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)

public static ConsoleKeyInfo ReadKey (
	bool intercept
)
public static ConsoleKeyInfo ReadKey (
	boolean intercept
)
public static function ReadKey (
	intercept : boolean
) : ConsoleKeyInfo
Not applicable.

Parameters

intercept

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

Return Value

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.

Exception typeCondition

InvalidOperationException

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.

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

// This example demonstrates the Console.ReadKey(Boolean) method
using System;
using System.Text;

class Sample 
{
    public static void Main() 
    {
    ConsoleKeyInfo cki;
//               0        1         2         3         4         5         6
//               123456789012345678901234567890123456879012345678901234567890
    String m1 = "This example discovers the console and modifier keys " +
                "that you press, \n" +
                "and optionally echoes the character that corresponds to " +
                "the console \n" +
                "key in column 1.\n";
    String m2 = "Press any combination of CTL, ALT, and SHIFT modifier keys, " +
                "and a console key.\n" +
                "Press the plus key (+) to toggle echoing the key character " +
                "in column 1.\n" +
                "Press the escape (Esc) key to quit: ";
    String m3 = "You pressed ";
    String m4 = " (character '{0}').";
    String m5 = "  (echo {0})";
    StringBuilder sb = new StringBuilder();
    Boolean suppress = false; 
//
// The Console.TreatControlCAsInput property prevents this example from
// ending if you press CTL+C, however all other operating system keys and 
// shortcuts, such as ALT+TAB or the Windows Logo key, are still in effect. 
//
    Console.TreatControlCAsInput = true;
    Console.WriteLine(m1);
    do 
    {
        Console.WriteLine(m2);
        sb.Length = 0;
        cki = Console.ReadKey(suppress);
        sb.Append(m3);
        if (cki.Modifiers != 0)
            {
            if ((cki.Modifiers & ConsoleModifiers.Alt) != 0)
                sb.Append("ALT+");
            if ((cki.Modifiers & ConsoleModifiers.Shift) != 0)
                sb.Append("SHIFT+");
            if ((cki.Modifiers & ConsoleModifiers.Control) != 0)
                sb.Append("CTL+");
            }
        sb.Append(cki.Key.ToString());
        sb.AppendFormat(m4, cki.KeyChar);
        sb.AppendLine().AppendLine();
        Console.WriteLine(m5, (suppress ? "OFF" : "ON"));
        Console.WriteLine(sb.ToString());
        if (cki.KeyChar == '+')
            {
            if (suppress) suppress = false;
            else          suppress = true;
            }
    } while (cki.Key != ConsoleKey.Escape);
    }
}
/*
This example produces results similar to the following text:

This example discovers the console and modifier keys that you press,
and optionally echoes the character that corresponds to the console
key in column 1.

Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
I  (echo ON)
You pressed SHIFT+I (character 'I').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
+  (echo ON)
You pressed Add (character '+').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
  (echo OFF)
You pressed SHIFT+I (character 'I').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
  (echo OFF)
You pressed Add (character '+').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
I  (echo ON)
You pressed SHIFT+I (character 'I').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
?  (echo ON)
You pressed Escape (character '?').

*/

// This example demonstrates the Console.ReadKey(Boolean) method
import System.*;
import System.Text.*;

class Sample
{
    public static void main(String[] args)
    {
        ConsoleKeyInfo cki;
        //      0        1         2         3         4         5         6
        //      123456789012345678901234567890123456879012345678901234567890
        String m1 = "This example discovers the console and modifier keys "
            + "that you press, \n"
            + "and optionally echoes the character that corresponds to "
            + "the console \n" + "key in column 1.\n";
        String m2 = "Press any combination of CTL, ALT, and SHIFT modifier keys, "
            + "and a console key.\n"
            + "Press the plus key (+) to toggle echoing the key character "
            + "in column 1.\n" + "Press the escape (Esc) key to quit: ";
        String m3 = "You pressed ";
        String m4 = " (character '{0}').";
        String m5 = "  (echo {0})";
        StringBuilder sb = new StringBuilder();
        System.Boolean suppress = false;
        //
        // The Console.TreatControlCAsInput property prevents this example from
        // ending if you press CTL+C, however all other operating system keys and 
        // shortcuts, such as ALT+TAB or the Windows Logo key, are still in
        // effect.
        //
        Console.set_TreatControlCAsInput(true);
        Console.WriteLine(m1);
        do {
            Console.WriteLine(m2);
            sb.set_Length(0);
            cki = Console.ReadKey(Convert.ToBoolean(suppress));
            sb.Append(m3);
            if (!(cki.get_Modifiers().Equals((Int32)0))) {
                if (!((cki.get_Modifiers() & ConsoleModifiers.Alt).ToString().
                    Equals(Convert.ToString(0)))) {
                    sb.Append("ALT+");
                }
                if (!((cki.get_Modifiers() & ConsoleModifiers.Shift).ToString().
                    Equals(Convert.ToString(0)))) {
                    sb.Append("SHIFT+");
                }
                if (!((cki.get_Modifiers() & ConsoleModifiers.Control).ToString().
                    Equals(Convert.ToString(0)))) {
                    sb.Append("CTL+");
                }
            }
            sb.Append(cki.get_Key().ToString());
            sb.AppendFormat(m4, (System.Char)cki.get_KeyChar());
            sb.AppendLine().AppendLine();
            Console.WriteLine(m5, (Convert.ToBoolean(suppress)) ? "OFF" : "ON");
            Console.WriteLine(sb.ToString());
            if (cki.get_KeyChar() == '+') {
                if (Convert.ToBoolean(suppress)) {
                    suppress = false;
                }
                else {
                    suppress = true;
                }
            }
        } while (!(cki.get_Key().Equals(ConsoleKey.Escape)));
    } //main
} //Sample
/*
This example produces results similar to the following text:

This example discovers the console and modifier keys that you press,
and optionally echoes the character that corresponds to the console
key in column 1.

Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
I  (echo ON)
You pressed SHIFT+I (character 'I').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
+  (echo ON)
You pressed Add (character '+').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
  (echo OFF)
You pressed SHIFT+I (character 'I').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
  (echo OFF)
You pressed Add (character '+').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
I  (echo ON)
You pressed SHIFT+I (character 'I').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
?  (echo ON)
You pressed Escape (character '?').

*/

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft