Detecting SINGLE key presses in CONSOLE C#

Viewed 2390

I'm new to coding and have decided to start with C#. I've decided to write a simple console program that will detect key press and if only Enter is pressed, it will show number. The problem is that you can just hold down key and it will continue to show numbers. What should I add to my code so program will detect only SINGLE presses and ignore if user is HOLDING the key? (I didn't find anything about this issue for CONSOLE C#, only for Forms one. Neither on this forum, nor in Web at all)

Thanks in advance

static void Main(string[] args)
{
    Console.WriteLine("Press Enter to play!");

    int num = 0;
    void WaitForKey(ConsoleKey key)
    {
        while (Console.ReadKey(true).Key != key)
        { }
    }

    for (int i = 0; i < 10; i++)
    {
        WaitForKey(ConsoleKey.Enter);
        Console.Write("{0} ", num);
        num++;
    }
}
4 Answers
Related