.NET Framework Class Library
Console..::.ReadKey Method

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

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

Visual Basic (Declaration)
<HostProtectionAttribute(SecurityAction.LinkDemand, UI := True)> _
Public Shared Function ReadKey As ConsoleKeyInfo
Visual Basic (Usage)
Dim returnValue As ConsoleKeyInfo

returnValue = Console.ReadKey()
C#
[HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)]
public static ConsoleKeyInfo ReadKey()
Visual C++
[HostProtectionAttribute(SecurityAction::LinkDemand, UI = true)]
public:
static ConsoleKeyInfo ReadKey()
JScript
public static function ReadKey() : ConsoleKeyInfo

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

ExceptionCondition
InvalidOperationException

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

Remarks

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.

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.

Examples

The following example demonstrates the parameterless ReadKey method.

Visual Basic
' This example demonstrates the Console.ReadKey() method
Imports System
Imports System.Text
Imports Microsoft.VisualBasic

Class Sample
   Public Shared Sub Main()
      Dim cki As ConsoleKeyInfo
      '                   0        1         2         3         4         5         6
      '                   123456789012345678901234567890123456879012345678901234567890
      Dim m1 As String = "This example discovers the console and modifier keys " & _
                         "that you press." & vbCrLf
      Dim m2 As String = "Press any combination of CTL, ALT, and SHIFT modifier keys, " & _
                         "and a console key." & vbCrLf & _
                         "Press the Escape (Esc) key to quit: "
      Dim m3 As String = "You pressed "
      Dim m4 As String = " (character '{0}')."
      Dim sb As New StringBuilder()
      '
      ' 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(True)
         sb.Append(m3)
         If cki.Modifiers <> 0 Then
            If(cki.Modifiers And ConsoleModifiers.Alt) <> 0 Then
               sb.Append("ALT+")
            End If
            If(cki.Modifiers And ConsoleModifiers.Shift) <> 0 Then
               sb.Append("SHIFT+")
            End If
            If(cki.Modifiers And ConsoleModifiers.Control) <> 0 Then
               sb.Append("CTL+")
            End If
         End If
         sb.Append(cki.Key.ToString())
         sb.AppendFormat(m4, cki.KeyChar)
         sb.AppendLine().AppendLine()
         Console.WriteLine(sb.ToString())
      Loop While cki.Key <> ConsoleKey.Escape
   End Sub 'Main ' Note: This example requires the Escape (Esc) key.
End Class 'Sample
'
'This example produces results similar to following text:
'
'This example discovers the console and modifier keys that you press.
'
'Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
'Press the Escape (Esc) key to quit:
'You pressed A (character 'a').
'
'
'Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
'Press the Escape (Esc) key to quit:
'You pressed SHIFT+A (character 'A').
'
'
'Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
'Press the Escape (Esc) key to quit:
'You pressed ALT+SHIFT+CTL+A (character ' ').
'
'
'Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
'Press the Escape (Esc) key to quit:
'You pressed Escape (character '?').
'
C#
// This example demonstrates the Console.ReadKey() 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";
    String m2 = "Press any combination of CTL, ALT, and SHIFT modifier keys, " +
                "and a console key.\nPress the Escape (Esc) key to quit: ";
    String m3 = "You pressed ";
    String m4 = " (character '{0}').";
    StringBuilder sb = new StringBuilder();   
//
// 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(true);
        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(sb.ToString());
    } while (cki.Key != ConsoleKey.Escape);
// Note: This example requires the Escape (Esc) key.
    }
}
/*
This example produces results similar to following text:

This example discovers the console and modifier keys that you press.

Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed A (character 'a').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed SHIFT+A (character 'A').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed ALT+SHIFT+CTL+A (character ' ').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed Escape (character '?').
*/
Visual C++
// This example demonstrates the Console.ReadKey() method
using namespace System;
using namespace System::Text;
int 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";
   String^ m2 = "Press any combination of CTL, ALT, and SHIFT modifier keys, "
   "and a console key.\nPress the Escape (Esc) key to quit: ";
   String^ m3 = "You pressed ";
   String^ m4 = " (character '{0}').";
   StringBuilder^ sb = gcnew StringBuilder;

   //
   // 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( true );
      sb->Append( m3 );
      if ( (int)cki.Modifiers )
      {
         if ( (int)(cki.Modifiers & ConsoleModifiers::Alt) )
                  sb->Append( "ALT+" );

         if ( (int)(cki.Modifiers & ConsoleModifiers::Shift) )
                  sb->Append( "SHIFT+" );

         if ( (int)(cki.Modifiers & ConsoleModifiers::Control) )
                  sb->Append( "CTL+" );
      }

      sb->Append( cki.Key.ToString() );
      sb->AppendFormat( m4, cki.KeyChar );
      sb->AppendLine()->AppendLine();
      Console::WriteLine( sb );
   }
   while ( cki.Key != ConsoleKey::Escape );


   // Note: This example requires the Escape (Esc) key.
}

/*
This example produces results similar to following text:

This example discovers the console and modifier keys that you press.

Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed A (character 'a').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed SHIFT+A (character 'A').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed ALT+SHIFT+CTL+A (character ' ').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed Escape (character '?').
*/
Platforms

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.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Tags :


Page view tracker