在控制台中扫描按键而不暂停

时间:2013-06-30 17:00:24

标签: c# windows dll console

我有这个问题。我有while循环在控制台中绘制文本,我想让它交互。我有两个if语句包含实际键的条件。 因此,如果我按'W'它会做某事,但那不会奏效。我有两个项目,engine.dll和testApp进行测试。下面是代码:

 while (true)
            {
                game g = new game();
                g.Draw(cHP, mHP, name,posX,posY,blip);
                Thread.Sleep(50);
                Console.CursorVisible = false;
                ConsoleKeyInfo cki = new ConsoleKeyInfo();
                //if (Console.KeyAvailable) { Console.CursorVisible = true; break; }
                if (cki.KeyChar == 'w') MoveY(1); // Here it is
                if (cki.KeyChar == 's') MoveY(-1); // and here
            }

1 个答案:

答案 0 :(得分:0)

在最终版本中不确定您想要的是什么,但对现有代码的修复将是

 Console.CursorVisible = false;
 while(true)
 {
       game g = new game();
       g.Draw(cHP, mHP, name,posX,posY,blip);
       Thread.Sleep(50); //might not be required depending
                         //on what you want to do

       var cki = Console.ReadKey(true);
       if (cki.KeyChar == 'w') MoveY(1);// Here it is
       if (cki.KeyChar == 's') MoveY(-1); // and here
 }
相关问题