无法从ReadKey()中读取一个字符

时间:2015-07-25 01:59:33

标签: c# console-application

我是c#的新手,我正在创建一个小的控制台应用程序。我想要一个平滑的菜单系统改变关键输入。我有一个循环注册

MenuItems =  await AppWinService.GetMenuEntriesAsync();   

if ((MenuItems != null) && (MenuItems.Any())
{
   ItemSource = new ObservableCollection<AlphaKeyGroup<Menu>>(
                                          (AlphaKeyGroup<Menu>.CreateGroups(MenuItems,
                                           CultureInfo.CurrentUICulture, 
                                           s => s.MenuName, 
                                           true)));

  ((CollectionViewSource)Resources["MenuGroups"]).Source = ItemSource;
}
else 
{ 
   MessageBox.Show("Failure to get data"); 
}

我有几个案例,案例'1'和案例'Q'。我第一次按任何一个它有效但下次我需要按两次和q三次。无论是SHIFT + q还是大写锁定。我尝试了许多不同的解决方案,清除缓冲区等,但还没有真正完成。

对我来说似乎很奇怪,但我想我错过了一些东西。有线索吗?感谢

1 个答案:

答案 0 :(得分:1)

所以完整的片段是:

{
    private static void Main(string[] args)
    {
        do
        {
            Console.WriteLine("Main Menu");
            Console.WriteLine("Press 1 for cars");
            Console.WriteLine("Press 2 for planes");
            Console.WriteLine("Press 3 for boats");
            Console.WriteLine("Enter 'q' to exit");
            ConsoleKeyInfo _key = Console.ReadKey();

            switch ((char) _key.Key)
            {
                case '1':
                {
                    Console.Clear();
                    CarMenu();
                    break;
                }
                case '2':
                {
                    Console.Clear();
                    //PlaneMenu();
                    break;
                }
                case '3':
                {
                    Console.Clear();
                    //BoatMenu();
                    break;
                }
                case 'Q':
                {
                    Environment.Exit(0);
                    break;
                }
                default:
                {
                    Console.WriteLine("No options to that key...");
                    Console.WriteLine("-------------------------\n\n");
                    break;
                }
            }
        } while (true);
    }

    public static void CarMenu()
    {
        do
        {
            Console.Clear();
            Console.WriteLine("Car menu");
            Console.WriteLine("Press 1 to list all cars");
            Console.WriteLine("Press 2 to list single car");
            Console.WriteLine("Press 3 to register a new car");
            Console.WriteLine("Press 0 for main menu");
            Console.WriteLine("Enter 'q' to exit");
            ConsoleKeyInfo _key = Console.ReadKey();
            Console.WriteLine((char) _key.Key);
            //Console.ReadKey();
            switch ((char) _key.Key)
            {
                case '1':
                {
                    Console.Clear();
                    Console.WriteLine("1");
                    //_handler.listAllDevices();
                    break;
                }
                case '2':
                {
                    Console.Clear();
                    Console.WriteLine("1");
                    break;
                }
                case '3':
                {
                    Console.Clear();
                    Console.WriteLine("1");
                    break;
                }
                case '0':
                {
                    Console.Clear();
                    return;
                }
                case 'Q':
                {
                    Environment.Exit(0);
                    return;
                }
                default:
                {
                    Console.WriteLine("No options to that key...");
                    Console.WriteLine("-------------------------\n\n");
                    break;
                }
            }
        } while (true);
    }
}

正如您所看到的,我已经第二次打电话给ReadKey()。这是原始问题。