当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Console.ReadKey()用法及代码示例


Console.ReadKey()方法使程序等待按键,并且在按键之前阻止屏幕。简而言之,它获取下一个字符或用户按下的任何键。按下的键将显示在控制台窗口中(如果将进行任何输入过程)。此方法的重载列表中有两种方法,如下所示:

    • ReadKey()方法
    • ReadKey(Boolean)方法

ReadKey() Method

此方法用于获取用户按下的下一个字符或函数键。按下的键显示在控制台窗口中。


用法: public static ConsoleKeyInfo ReadKey ();

返回值:此方法返回一个对象,该对象描述ConsoleKey常量和Unicode字符(如果有),它对应于按下的键。

异常:如果In属性属于某个不是控制台的流,则此方法将提供InvalidOperationException。 “In”属性用于获取标准输入流。

以下示例程序旨在说明上述方法的用法:

示例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 ask for 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 for press "E" for exit 
        while (Console.ReadKey().Key != ConsoleKey.E) { 
        } 
          
    } 
}

输出:

ReadKey(Boolean) Method

此方法与先前的方法更相似,也就是说,它还获得用户按下的下一个字符或任何键。唯一的区别是所选择的键可以有选择地显示在控制台窗口中。

用法: 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.

返回值:此方法返回一个对象,该对象描述ConsoleKey常量和Unicode字符(如果有),它对应于按下的键。

异常:当In属性属于某个不是控制台的流时,此方法将提供InvalidOperationException。 “In”属性用于获取标准输入流。

以下示例程序旨在说明上述方法的用法:

示例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" for exit 
        // The key "E" is shown in the console  
        // output window because of "false" 
        while (Console.ReadKey(false).Key != ConsoleKey.E) { 
        } 
          
    } 
}

输出:



相关用法


注:本文由纯净天空筛选整理自SoumikMondal大神的英文原创作品 Console.ReadKey() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。