ConsoleKey Enum

Represents the keys on the console.

Summary

This enumeration defines the keys available on a console. It is used by the Console.ReadKey method to return information about the key that was pressed.

Namespace

System

Members

Usage Example


using System;

public class Example
{
    public static void Main()
    {
        Console.WriteLine("Press any key...");
        ConsoleKeyInfo keyInfo = Console.ReadKey();

        Console.WriteLine($"\nYou pressed the '{keyInfo.KeyChar}' key.");
        Console.WriteLine($"Key: {keyInfo.Key}");
        Console.WriteLine($"Modifiers: {keyInfo.Modifiers}");

        if (keyInfo.Key == ConsoleKey.Escape)
        {
            Console.WriteLine("Escape key was pressed. Exiting.");
        }
    }
}