简体   繁体   English

如何使用 c# 中的按键来浏览控制台应用程序?

[英]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.我是 C# 的新手,我正在尝试创建一个小型控制台应用程序。 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 .在这个应用程序中,我想实现一个欢迎屏幕,在该屏幕中,用户会很高兴并要求按Escape等键退出或Enter以继续应用程序菜单

I'm wondering if there's a way to display only the menu after the user wants to press Enter key.我想知道在用户想要按Enter键后是否有办法只显示菜单 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();
            }

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#控制台应用程序中使用IsolatedStorage - How to use IsolatedStorage in a C# Console Application 如何在控制台应用程序中使用 ShowFileDialog c# - How to use ShowFileDialog in Console Application c# 如何通过C#控制台应用程序更新MySql表行? - How to update MySql table row through C# console application? 如何通过C#控制台应用程序读取邮件内容 - How to read mail content through c# console application C# 控制台应用程序中的 WebBrowser 无法导航 - WebBrowser in C# console application won't navigate C# 应用程序在控制台中使用另一个应用程序 - C# application to use another application in Console 如何在C#控制台应用程序中使用Microsoft图形资源管理器? - How to use the Microsoft graph explorer in c# console application? 在C#控制台应用程序中,如何在表中使用\\ n? - In a C# Console Application, how do you use \n in a table? 如何在C#的控制台应用程序中将计时器与Web浏览器一起使用? - How to use timer with web-browser in console application in c#? 如何在C#控制台应用程序中使用配置替换 - How Can I Use Configuration Replacements for C# Console Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM