C#中console.readkey()的用法是什么

avatar
作者
筋斗云
阅读量:0

Console.ReadKey()是一个在控制台应用程序中读取单个按键的方法。它会等待用户输入一个键后返回。以下是它的用法示例:

using System;  class Program {     static void Main()     {         Console.WriteLine("Press any key to continue...");         Console.ReadKey();         Console.WriteLine("Key pressed!");     } } 

在这个示例中,程序会在输出一条消息后等待用户按下任意键。一旦用户按下键后,它将继续执行并输出另一条消息。

ReadKey()方法还可以返回一个ConsoleKeyInfo对象,该对象包含按键的详细信息,例如按键的字符值、修饰键(如Shift、Ctrl)的状态等。你可以使用这些信息做更多的处理。

using System;  class Program {     static void Main()     {         Console.WriteLine("Press any key and see the details...");         ConsoleKeyInfo keyInfo = Console.ReadKey();                  Console.WriteLine("\nKey: " + keyInfo.Key);         Console.WriteLine("Char: " + keyInfo.KeyChar);         Console.WriteLine("Modifiers: " + keyInfo.Modifiers);     } } 

在这个示例中,程序会等待用户按下任意键,并在按下键后显示按键的详细信息,如按键的值、字符值和修饰键的状态。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!