0

Basically I need to know how to test if the user presses the number 1 on their keyboard for the console to then execute a command. If they don't, they'd have to press 2 or 3, etc. for another command to be executed.

using System;
using System.Threading;
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello,[user]!");
        Console.WriteLine("Please Enter Your Username");
        string uname = Console.ReadLine();
        Thread.Sleep(1000);
        Console.Clear();
        Thread.Sleep(1500);
        Console.WriteLine("Welcome to Axiom, " + uname);
        Console.WriteLine("If You Want To Find The Full List Of Commands, Please Press 1");
        Console.WriteLine("If Not, Then Press Esc To Exit Or Press A Command Button To Execute A Command");
        
    }
}
Blue_A55T
  • 21
  • 4
  • 1
    Does [C# Console app - How do I make an interactive menu?](https://stackoverflow.com/questions/60767909/c-sharp-console-app-how-do-i-make-an-interactive-menu) help? – Andrew Morton Nov 04 '21 at 16:23
  • Are you looking for Console.Read() https://learn.microsoft.com/en-us/dotnet/api/system.console.read?view=net-5.0 – Gaurav Mathur Nov 04 '21 at 16:25
  • This has been asked a ton of times. At least try a [Google Search](https://www.google.com/search?q=c%23+console+user+input+site:stackoverflow.com). – Metro Smurf Nov 04 '21 at 16:27

2 Answers2

1

Try this:

    Console.WriteLine("Hello,[user]!");
    Console.WriteLine("Please Enter Your Username");
    string uname = Console.ReadLine();
    Thread.Sleep(1000);
    Console.Clear();
    Thread.Sleep(1500);
    Console.WriteLine("Welcome to Axiom, " + uname);
    Console.WriteLine("If You Want To Find The Full List Of Commands, Please Press 1");
    if(Console.ReadKey(true).KeyChar == Convert.ToChar("1"))
    {
     //More Code...
    }
    else
    {
       Console.WriteLine("If Not, Then Press Esc To Exit Or Press A Command Button To Execute A Command");
       //more code...
    }
1

You read a single character using Console.ReadKey(bool b). A switch statement can do the rest. E.g:

            Console.WriteLine("Hello,[user]!");
            Console.WriteLine("Please Enter Your Username");
            string uname = Console.ReadLine();
            Thread.Sleep(1000);
            Console.Clear();
            Thread.Sleep(1500);
            Console.WriteLine("Welcome to Axiom, " + uname);
            Console.WriteLine("If You Want To Find The Full List Of Commands, Please Press 1");
            Console.WriteLine("If Not, Then Press Esc To Exit Or Press A Command Button To Execute A Command");
            char c = Console.ReadKey(true).KeyChar;
            switch( c )
            {
                case '1': 
                    // do some work
                    break;
                case 'A': 
                    // do some other work
                    break;
                case '\x1B': // this is ESC
                    return;
                default: 
                    Console.WriteLine("Wrong character!");
                    break;
            }

Parameter true in the call to ReadKey() prevents the user entry from being echoed to the console. Set this parameter to false, or skip it, if you want the user to see the entered character.

Kalle Svensson
  • 353
  • 1
  • 10