Console.KeyAvailable - 如何重置此布尔值?

时间:2013-03-29 17:28:38

标签: c# boolean

在这里使用C#。我正在创建一个控制台应用程序。我有两种滚动文本的方法,而不是立即在屏幕上显示它。一个与Console.Write()的工作方式相同,另一个的工作方式与Console.WriteLine()相同。不同之处在于每个角色之间有30毫秒的延迟。我试图这样做,以便当你按住一个键时,每个角色之间的延迟加速达5ms。问题是,一旦你按下一个键,Console.KeyAvailable就为真,不会重置,因此它只会延迟5ms。有没有办法把它设置为假,或者我可以做些什么来实现这个目标?这是我的代码:

    //Console.Write() version
    static void RPGWrite(string write)
    {
        char[] writearray = write.ToCharArray();
        int writearraycount = writearray.Count();
        for (int x = 0; x < writearraycount; x++)
        {
            Console.Write(Convert.ToString(writearray[x]));
            if (Console.KeyAvailable == false)
                System.Threading.Thread.Sleep(30);
            else
                System.Threading.Thread.Sleep(5);                   
        }            
    }
    //Console.WriteLine() version
    static void RPGWriteLine(string write)
    {
        char[] writearray = write.ToCharArray();
        int writearraycount = writearray.Count();
        for (int x = 0; x < writearraycount; x++)
        {
            Console.Write(Convert.ToString(writearray[x]));
            if (Console.KeyAvailable == false)
                System.Threading.Thread.Sleep(30);
            else
                System.Threading.Thread.Sleep(5);
        }
        Console.Write("\n");
    }

2 个答案:

答案 0 :(得分:4)

[nkvu - 将评论中的项目移至此处,以防其他人帮助其他人]

AFAIK,要将KeyAvailable设置为false,您实际上需要从流中读取密钥(例如,使用Console.ReadKey)。

使用Console.ReadKey(true)将从流中读取一个字符,而不会回显到控制台以供用户查看 - 尽管请注意MSDN文档中关于按键Alt,Ctrl或Shift键的注释。

答案 1 :(得分:1)

这是最终为我工作的代码:

    //Console.Write() version
    static void RPGWrite(string write)
    {
        char[] writearray = write.ToCharArray();
        int writearraycount = writearray.Count();
        for (int x = 0; x < writearraycount; x++)
        {
            if (Console.KeyAvailable == false)
            {
                Console.Write(Convert.ToString(writearray[x]));
                System.Threading.Thread.Sleep(30);
            }
            else
            {
                Console.Write(Convert.ToString(writearray[x]));
                if (x < (writearraycount - 1))
                {
                    x++;
                    Console.Write(Convert.ToString(writearray[x]));
                }
                if (x < (writearraycount - 2))
                {
                    x++;
                    Console.Write(Convert.ToString(writearray[x]));
                }
                if (x < (writearraycount - 3))
                {
                    x++;
                    Console.Write(Convert.ToString(writearray[x]));
                }
                if (x < (writearraycount - 4))
                {
                    x++;
                    Console.Write(Convert.ToString(writearray[x]));
                }
                Console.ReadKey(true);
            }
        }           
    }
    //Console.WriteLine() version
    static void RPGWriteLine(string write)
    {
        char[] writearray = write.ToCharArray();
        int writearraycount = writearray.Count();
        for (int x = 0; x < writearraycount; x++)
        {
            if (Console.KeyAvailable == false)
            {
                Console.Write(Convert.ToString(writearray[x]));
                System.Threading.Thread.Sleep(30);
            }
            else
            {
                Console.Write(Convert.ToString(writearray[x]));
                if (x < (writearraycount - 1))
                {
                    x++;
                    Console.Write(Convert.ToString(writearray[x]));
                }
                if (x < (writearraycount - 2))
                {
                    x++;
                    Console.Write(Convert.ToString(writearray[x]));
                }
                if (x < (writearraycount - 3))
                {
                    x++;
                    Console.Write(Convert.ToString(writearray[x]));
                }
                if (x < (writearraycount - 4))
                {
                    x++;
                    Console.Write(Convert.ToString(writearray[x]));
                }
                Console.ReadKey(true);
            }
        }
        Console.Write("\n");
    }