Console.ReadKey() Method in C#
Console.ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed. In short, it obtains the next character or any key pressed by the user. The pressed key is displayed in the console window(if any input process will happen). There are two methods in the overload list of this method as follows:
- ReadKey() Method
- ReadKey(Boolean) Method
ReadKey() Method
This method is used to get the next character or function key pressed by the user. The pressed key is displayed in the console window.
Syntax: public static ConsoleKeyInfo ReadKey ();
Return Value: This method returns an object that describes the ConsoleKey constant and Unicode character(if any), it corresponds to the pressed key.
Exception: This method will give InvalidOperationException if the In property is belongs to some stream which is other than the console. “In” property is use to take standard input stream.
Below programs illustrate the use of the above-discussed method:
Example 1:
using System;
class GFG {
public static void Main()
{
int c = 0;
Console.WriteLine( "The series is:" );
for ( int i = 1; i < 10; i++)
{
c = c + i;
Console.Write(c + " " );
}
Console.WriteLine( "\npress any key to exit the process..." );
Console.ReadKey();
}
}
|
Output:
Example 2:
using System;
class GFG {
public static void Main()
{
int c = 0;
Console.WriteLine( "The series is:" );
for ( int i = 1; i < 10; i++)
{
c = c + i;
Console.Write(c + " " );
}
Console.Write( "\nPress 'Enter' to exit the process..." );
while (Console.ReadKey().Key != ConsoleKey.Enter) {
}
}
}
|
Output:
Example 3:
using System;
class GFG {
public static void Main()
{
DateTime d = DateTime.Now;
Console.WriteLine( "System date: {0:d}\n" +
"System time: {0:t}" , d);
Console.Write( "Press 'E' to exit the process..." );
while (Console.ReadKey().Key != ConsoleKey.E) {
}
}
}
|
Output:
ReadKey(Boolean) Method
This method is more similar to the previous method, that is, it also obtains the next character or any key pressed by the user. The only difference is that the pressed key is optionally displayed in the console window.
Syntax: public static ConsoleKeyInfo ReadKey (bool key);
Here, “key” is used to determines whether to display the pressed key in the console window. If “true” then the pressed key will not be shown in the output window. If “false” then the pressed key will be shown in the output window.
Return Value: This method returns an object that describes the ConsoleKey constant and Unicode character(if any), it correspond to the pressed key.
Exception: This method will give InvalidOperationException when the In property is belongs to some stream which is other than the console. “In” property is use to take standard input stream.
Below programs illustrate the use of the above-discussed method:
Example 1:
using System;
class GFG {
public static void Main()
{
int c = 0;
Console.WriteLine( "The series is-" );
for ( int i = 1; i < 10; i++)
{
c = c + i;
Console.Write(c + " " );
}
Console.WriteLine( "\npress any key to exit the process..." );
Console.ReadKey( false );
}
}
|
Output:
Examples 2:
using System;
class GFG {
public static void Main()
{
int c = 0;
Console.WriteLine( "The series is-" );
for ( int i = 1; i < 10; i++)
{
c = c + i;
Console.Write(c + " " );
}
Console.Write( "\nPress 'E' to exit the process..." );
while (Console.ReadKey( true ).Key != ConsoleKey.E) {
}
}
}
|
Output:
Examples 3:
using System;
class GFG {
public static void Main()
{
DateTime d = DateTime.Now;
Console.WriteLine( "System date: {0:d}\n" +
"System time: {0:t}" , d);
Console.Write( "Press 'E' to exit the process..." );
while (Console.ReadKey( false ).Key != ConsoleKey.E) {
}
}
}
|
Output:
Last Updated :
17 Feb, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...