How to use ReadKey() method of Console class in C#?

The Console.ReadKey() method in C# reads the next character or function key pressed by the user. This method is commonly used to pause program execution until the user presses a key, which is particularly useful when running console applications from Visual Studio to prevent the console window from closing immediately.

Syntax

The Console.ReadKey() method has two overloads −

public static ConsoleKeyInfo ReadKey();
public static ConsoleKeyInfo ReadKey(bool intercept);

Parameters

  • intercept (optional): A boolean value that determines whether to display the pressed key in the console. If true, the key is not displayed; if false or omitted, the key is displayed.

Return Value

Returns a ConsoleKeyInfo object that describes the pressed key, including the character value and modifier keys (Shift, Alt, Ctrl).

Using ReadKey() to Pause Program Execution

The most common use of ReadKey() is to pause program execution until the user presses any key −

using System;

public class Demo {
    public static void Main() {
        Console.WriteLine("Hello, World!");
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
        Console.WriteLine("Program continues after key press.");
    }
}

The output of the above code is −

Hello, World!
Press any key to continue...
Program continues after key press.

Using ReadKey() with Key Detection

You can detect which specific key was pressed and perform different actions −

using System;

public class KeyDetectionDemo {
    public static void Main() {
        Console.WriteLine("Press a key (ESC to exit):");
        
        ConsoleKeyInfo keyInfo;
        do {
            keyInfo = Console.ReadKey(true);
            
            if (keyInfo.Key == ConsoleKey.A) {
                Console.WriteLine("You pressed 'A'");
            } else if (keyInfo.Key == ConsoleKey.Enter) {
                Console.WriteLine("You pressed Enter");
            } else if (keyInfo.Key != ConsoleKey.Escape) {
                Console.WriteLine($"You pressed: {keyInfo.Key}");
            }
        } while (keyInfo.Key != ConsoleKey.Escape);
        
        Console.WriteLine("Goodbye!");
    }
}

The output of the above code is −

Press a key (ESC to exit):
You pressed 'A'
You pressed Enter
You pressed: D
Goodbye!

Using ReadKey() with Intercept Parameter

The intercept parameter controls whether the pressed key appears on the console −

using System;

public class InterceptDemo {
    public static void Main() {
        Console.WriteLine("Type some characters:");
        Console.WriteLine("First key (visible):");
        ConsoleKeyInfo key1 = Console.ReadKey(false);
        
        Console.WriteLine("\nSecond key (hidden):");
        ConsoleKeyInfo key2 = Console.ReadKey(true);
        
        Console.WriteLine($"\nYou pressed: {key1.KeyChar} (visible) and {key2.KeyChar} (hidden)");
    }
}

The output of the above code is −

Type some characters:
First key (visible):
A
Second key (hidden):
You pressed: A (visible) and B (hidden)

Comparison of Console Input Methods

Method Input Type Blocks Until
ReadKey() Single key press Any key is pressed
ReadLine() Full line of text Enter key is pressed
Read() Single character Character is entered

Conclusion

The Console.ReadKey() method is essential for creating interactive console applications in C#. It allows you to pause program execution, detect specific key presses, and control whether the pressed key is displayed on the console, making it perfect for menu systems and user input validation.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements