3

在这里使用 C#。我正在创建一个控制台应用程序。我有两种方法可以滚动文本,而不是立即在屏幕上显示它。一个与 Console.Write() 相同,另一个与 Console.WriteLine() 相同。不同之处在于每个字符之间有 30 毫秒的延迟。我正在努力做到这一点,以便当您按住一个键时,每个字符之间的延迟会加快 5 毫秒。问题是,一旦你按下一个键,Console.KeyAvailable 就为真并且不会重置,因此它只会延迟 5 毫秒。有没有办法将它设置回false,或者我可以做些什么来完成这个?这是我的代码:

    //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");
    }
4

2 回答 2

4

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

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

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

于 2013-03-29T18:12:56.860 回答
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");
    }
于 2013-04-01T17:23:14.863 回答