Home > Mobile >  Press space bar to continue
Press space bar to continue

Time:11-01

I want the user to press the spacebar to see the next message in my ConsoleApp:

Console.WriteLine("a sentence");

//press space to see next sentence
       
Console.WriteLine("another sentence");

//press space to see next sentence
      
Console.WriteLine("etc");

If possible the user should not visualize any prompt.

Thanks!

CodePudding user response:

using System;

namespace DemoApp
{
    internal class Program
    {
        static void WaitForSpace()
        {
            while (Console.ReadKey(true).Key != ConsoleKey.Spacebar) ;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("a sentence");
            WaitForSpace();
            Console.WriteLine("another sentence");
            WaitForSpace();
            Console.WriteLine("etc");
            WaitForSpace();
        }
    }
}

CodePudding user response:

You can loop reading characters until space:

Console.WriteLine("a sentence");
while (Console.ReadKey(true).KeyChar != ' ') ;
       
Console.WriteLine("another sentence");
while (Console.ReadKey(true).KeyChar != ' ') ;
      
Console.WriteLine("etc");

Alternatively you can create a method that comprises the while statement and call it instead.

CodePudding user response:

Try this:

Console.WriteLine("a sentence");
if(Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
      Console.WriteLine("another sentence");
      if(Console.ReadKey(true).Key == ConsoleKey.Spacebar)
      {
           Console.WriteLine("etc");
      }
}
  •  Tags:  
  • c#
  • Related