Search code examples
c#menuconsoleuser-input

How do I get my methods to work until I have pressed the esc key to exit the console?


namespace Keyboard_Menu_Demo
{
    class Menu
    {
        public void MenuOptions()
        {
            ConsoleKeyInfo KeyPressed = Console.ReadKey(true);
            if (KeyPressed.Key == ConsoleKey.W)
                {
                    Console.WriteLine("You pressed the W button~!");
                }
            if (KeyPressed.Key == ConsoleKey.S)
                {
                    Console.WriteLine("You pressed the S button~!");
                }
            if (KeyPressed.Key == ConsoleKey.A)
                {
                    Console.WriteLine("You pressed the A button~!");
                }
            if (KeyPressed.Key == ConsoleKey.D)
                {
                    Console.WriteLine("You pressed the D button~!");
                }
            while (KeyPressed.Key != ConsoleKey.Escape) { }
        }
    }
}

namespace Keyboard_Menu_Demo
{
     class Game
    {
        public void Start()
        {
            
            Console.WriteLine("The game is starting.");
            Console.WriteLine("Press <esc> to exit.");
            
        }
    }
}
namespace Keyboard_Menu_Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Game mygame = new Game();
            Menu mymenu = new Menu();
            mygame.Start();
            mymenu.MenuOptions();
            
        }
    }
}

MenuOptions Method only works for one key press in the console. After that, I can't get a test-message to come through.

Start Method doesn't allow me to exit using esc key.

I tried to move the empty while loop around to the Main Entry Point and below the line of code calling the MenuOptions method, thinking it would affect the actual run of the program- Diiiid noooot...

I was expecting my code to allow me to trigger messages from pressing keys until I pressed the escape key. That still is the goal.

I barely know what I am doing. I haven't gotten any projects done yet, does printing Hello World count? lol Working on a tic-tac-toe game, going ok. I have gotten better from being totally jarred by coding concepts, but I get pretty lost when it comes to anything more than a few tricks I've copied. I am really out of my zone just started self-teaching so honestly any outside input probably would help me!


Solution

  • You "reading" pressed key only once at MenuOptions(). If the key is W/A/S/D - it would print specified output, elseway not, and then will go into infinite while loop.

    You should infinitely ReadKey in while loop and then check pressed keys and perform neccessary actions:

    public void MenuOptions()
    {
        // Declare variable to store last pressed key
        ConsoleKey pressedKey;
    
        // Run infinite loop, until pressed key is Escape
        while ((pressedKey = Console.ReadKey().Key) != ConsoleKey.Escape)
        {
            // and now performs your actions based on pressed key, except Excape
            switch (pressedKey)
            {
                case ConsoleKey.W:
                case ConsoleKey.A:
                case ConsoleKey.S:
                case ConsoleKey.D:
                    Console.WriteLine($"You pressed the {pressedKey} key");
                    break;
                case ConsoleKey.Y:
                    Console.WriteLine("Y you pressed Y key? Are you joking with me?");
                    break;
                default:
                    break;
                
            }
        }
    
    }