0

I need to check if spacebar or another key is pressed in a foreach (console application), something like this:

public static void WriteMessage(string text)
{
    int speed = 150;
    foreach (char c in text)
    {
        Console.Write(c);
        if (Console.ReadKey().Key==ConsoleKey.Spacebar)
        {
            speed = 10;
        }
        else
        {
            speed = 150;
        }
        Thread.Sleep(speed);
    }
}

but if i use Console.ReadKey() in the if(), the loop stops in this point, waiting a key pression

Sorry for bad english

  • Check for `Console.KeyAvailable` in if, if that is true, then you can read a key, if its false, no key is pressed. – Ron Beyer Jun 02 '15 at 17:42
  • You'll need a parallel thread for this – Shaharyar Jun 02 '15 at 17:42
  • [Listen for key press in .NET console app](http://stackoverflow.com/questions/5891538/listen-for-key-press-in-net-console-app) – Behzad Jun 02 '15 at 17:47
  • [c# reading user input without stopping an app](http://stackoverflow.com/questions/3931258/c-sharp-reading-user-input-without-stopping-an-app) – Behzad Jun 02 '15 at 17:48

0 Answers0