简体   繁体   中英

How to use keys in c# to navigate through a console application?

I'm new to C# and I'm trying to creat a small console application. In this application I want to implement a Welcome Screen in which the user would be greated and asked to press a key like Escape to exit or Enter to continue the application menu .

I'm wondering if there's a way to display only the menu after the user wants to press Enter key. I would be happy for any help. Thank you.

Here's the example of what I'm trying to do:

static void Main(string[] args)
        {
            ConsoleKeyInfo key;

            Console.WriteLine("Welcome! \n\n");
            Console.WriteLine("Press ENTER to continue or ESC to quit.");

            while (true)
            {
                key = Console.ReadKey(true);

                if (key.Key == ConsoleKey.Escape)
                {
                    break;
                }
                else if (key.Key == ConsoleKey.Enter)
                {
                    Menu menu = new Menu();
                    menu.demo();
                }

            }
        }

I think that's what you mean

static void Main(string[] args)
    {
        ConsoleKeyInfo key;
        bool isEnter = true;
        while (isEnter)
        {
            Console.WriteLine("----------------------------");
            Console.WriteLine("Welcome! \n");
            Console.WriteLine("Press ENTER to continue or ESC to quit.");
            key = Console.ReadKey(true);

            if (key.Key == ConsoleKey.Escape)
            {
                isEnter = false;
                break;
            }
            else if (key.Key == ConsoleKey.Enter)
            {

                Console.Clear();
                Menu menu = new Menu();
                menu.demo();
            }

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM