我如何使用console.keyavailable。 [英] How do I use console.keyavailable.

查看:273
本文介绍了我如何使用console.keyavailable。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测是否有任何按键被按下,但据我所知,if语句没有被触发。



工作是正在打印,因此正在调用GameLoop()。



不确定该怎么做...感谢您的帮助!



我尝试过:



I am wanting to detect if any key is being pressed but as far as I can tell the if statement isn't being triggered.

"working" is being printed so GameLoop() is being called.

Not sure what to do.. Thanks for the help!

What I have tried:

public static int GameLoop()
           {
               while(true)
               {
                   if(Console.KeyAvailable == true)
                   {
                       Environment.Exit(0); //test to see if being used
                       Player.KeyPressed = Console.ReadKey().KeyChar;
                       Player.Movement();
                   }

                   Console.WriteLine("working");

推荐答案

可能是导致问题的WriteLine:这将经常发生输出到 - 控制台部分非常繁忙,它支持高天堂。



使用DateTime.Now来检测秒数的时间,而不是一直输出改变了,而是每秒输出一行。我怀疑KeyAvailable会神奇地开始工作......
Probably, it's the WriteLine that is causing the problem: that will be done so often that the output-to-the-console part is so busy it's backed up to high heaven.

Instead of outputting all the time, use DateTime.Now to detect when the number of seconds has changed, and output one line per second instead. I suspect that KeyAvailable will magically start to work...


参见 Console.KeyAvailable属性(系统) [ ^ ]。


这篇关于我如何使用console.keyavailable。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆