Console.ReadKey()方法使程序等待按键,并且直到按键被按下才阻止屏幕显示。简而言之,它获取下一个字符或用户按下的任何键。按下的键将显示在控制台窗口中(如果将进行任何输入过程)。此方法的重载列表中有两种方法,如下所示:
- ReadKey()方法
- ReadKey(Boolean)方法
ReadKey()方法
此方法用于获取用户按下的下一个字符或函数键。按下的键显示在控制台窗口中。
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.
下面的程序说明了上述方法的用法:
范例1:
// C# program to illustrate the
// Console.ReadKey Method
using System;
class GFG {
// Main Method
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...");
// basic use of "Console.ReadKey()" method
Console.ReadKey();
}
}
输出:
范例2:
// C# program to illustrate the
// Console.ReadKey Method
using System;
class GFG {
// Main Method
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...");
// another use of "Console.ReadKey()" method
// here it asks to press the enter key to exit
while (Console.ReadKey().Key != ConsoleKey.Enter) {
}
}
}
输出:
范例3:
// C# program to illustrate the
// Console.ReadKey Method
using System;
class GFG {
// Main Method
public static void Main()
{
// "DateTime" is a inbuilt class
// for date and time
DateTime d = DateTime.Now;
// print the system date and time
Console.WriteLine("System date: {0:d}\n"+
"System time: {0:t}", d);
Console.Write("Press 'E' to exit the process...");
// here it ask to press "E" to exit
while (Console.ReadKey().Key != ConsoleKey.E) {
}
}
}
输出:
ReadKey(Boolean)方法
此方法与先前的方法更相似,也就是说,它还获得用户按下的下一个字符或任何键。唯一的区别是所选择的键可以有选择地显示在控制台窗口中。
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.
下面的程序说明了上述方法的用法:
范例1:
// C# program to illustrate the
// ReadKey(Boolean) Method
using System;
class GFG {
// Main Method
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...");
// here we use "false" in the argument list
// when we press any key, the key will
// displays in the console output window
Console.ReadKey(false);
}
}
输出:
范例2:
// C# program to illustrate the
// ReadKey(Boolean) Method
using System;
class GFG {
// Main Method
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...");
// here it asks to press "E" to exit
// and the key "E" is not shown in
// the console output window
while (Console.ReadKey(true).Key != ConsoleKey.E) {
}
}
}
输出:
范例3:
// C# program to illustrate the
// ReadKey(Boolean) Method
using System;
class GFG {
public static void Main()
{
// "DateTime" is a inbuilt class
// for date and time
DateTime d = DateTime.Now;
// print the system date and time
Console.WriteLine("System date: {0:d}\n"+
"System time: {0:t}", d);
Console.Write("Press 'E' to exit the process...");
// here it asks to press "E" to exit
// The key "E" is shown in the console
// output window because of "false"
while (Console.ReadKey(false).Key != ConsoleKey.E) {
}
}
}
输出: