當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。