Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System
Console Class
Console Properties
 KeyAvailable Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
Console.KeyAvailable Property

Note: This property is new in the .NET Framework version 2.0.

Gets a value indicating whether a key press is available in the input stream.

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

Visual Basic (Declaration)
Public Shared ReadOnly Property KeyAvailable As Boolean
Visual Basic (Usage)
Dim value As Boolean

value = Console.KeyAvailable
C#
public static bool KeyAvailable { get; }
C++
public:
static property bool KeyAvailable {
    bool get ();
}
J#
/** @property */
public static boolean get_KeyAvailable ()
JScript
public static function get KeyAvailable () : boolean

Property Value

true if a key press is available; otherwise, false.
Exception typeCondition

IOException

An I/O error occurred.

InvalidOperationException

Standard input is redirected to a file instead of the keyboard.

The property value is returned immediately; that is, the KeyAvailable property does not block input until a key press is available.

Use the KeyAvailable property in conjunction with only the ReadKey method, not the Read or ReadLine methods.

Visual Basic
' This example demonstrates the Console.KeyAvailable property.

Imports System
Imports System.Threading
Imports Microsoft.VisualBasic

Class Sample
   Public Shared Sub Main()
      Dim cki As New ConsoleKeyInfo()
      
      Do
         Console.WriteLine(vbCrLf & "Press a key to display; press the 'x' key to quit.")

' Your code could perform some useful task in the following loop. However, 
' for the sake of this example we'll merely pause for a quarter second.

         While Console.KeyAvailable = False
            Thread.Sleep(250) ' Loop until input is entered.
         End While
         cki = Console.ReadKey(True)
         Console.WriteLine("You pressed the '{0}' key.", cki.Key)
      Loop While cki.Key <> ConsoleKey.X
   End Sub 'Main
End Class 'Sample

'This example produces results similar to the following text:
'
'Press a key to display; press the 'x' key to quit.
'You pressed the 'H' key.
'
'Press a key to display; press the 'x' key to quit.
'You pressed the 'E' key.
'
'Press a key to display; press the 'x' key to quit.
'You pressed the 'PageUp' key.
'
'Press a key to display; press the 'x' key to quit.
'You pressed the 'DownArrow' key.
'
'Press a key to display; press the 'x' key to quit.
'You pressed the 'X' key.
'
C#
// This example demonstrates the Console.KeyAvailable property.
using System;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
    ConsoleKeyInfo cki = new ConsoleKeyInfo();

    do {
        Console.WriteLine("\nPress a key to display; press the 'x' key to quit.");

// Your code could perform some useful task in the following loop. However, 
// for the sake of this example we'll merely pause for a quarter second.

        while (Console.KeyAvailable == false)
            Thread.Sleep(250); // Loop until input is entered.
        cki = Console.ReadKey(true);
        Console.WriteLine("You pressed the '{0}' key.", cki.Key);
        } while(cki.Key != ConsoleKey.X);
    }
}
/*
This example produces results similar to the following text:

Press a key to display; press the 'x' key to quit.
You pressed the 'H' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'E' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'PageUp' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'DownArrow' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'X' key.
*/
C++
// This example demonstrates the Console.KeyAvailable property.
using namespace System;
using namespace System::Threading;
int main()
{
   ConsoleKeyInfo cki;
   do
   {
      Console::WriteLine( "\nPress a key to display; press the 'x' key to quit." );
      
      // Your code could perform some useful task in the following loop. However, 
      // for the sake of this example we'll merely pause for a quarter second.
      while ( Console::KeyAvailable == false )
            Thread::Sleep( 250 );
      cki = Console::ReadKey( true );
      Console::WriteLine( "You pressed the '{0}' key.", cki.Key );
   }
   while ( cki.Key != ConsoleKey::X );
}

/*
This example produces results similar to the following text:

Press a key to display; press the 'x' key to quit.
You pressed the 'H' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'E' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'PageUp' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'DownArrow' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'X' key.
*/
J#
// This example demonstrates the Console.KeyAvailable property.
import System.*;
import System.Threading.*;

class Sample
{
    public static void main(String[] args)
    {
        ConsoleKeyInfo cki = new ConsoleKeyInfo();
        do {
            Console.WriteLine("\nPress a key to display; press the 'x' key to"
                + " quit.");
            // Your code could perform some useful task in the following loop.
            // However,for the sake of this example we'll merely pause for a 
            // quarter second.
            while (Console.get_KeyAvailable() == false) {
                System.Threading.Thread.Sleep(250); // Loop until input is
                                                    // entered.
            }
            cki = Console.ReadKey(true);
            Console.WriteLine("You pressed the '{0}' key.", cki.get_Key());
        } while (!(cki.get_Key().Equals(ConsoleKey.X)));
    } //main
} //Sample
/*
This example produces results similar to the following text:

Press a key to display; press the 'x' key to quit.
You pressed the 'H' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'E' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'PageUp' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'DownArrow' key.

Press a key to display; press the 'x' key to quit.
You pressed the 'X' key.
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker