【发布时间】:2013-03-20 10:51:22
【问题描述】:
在这里使用 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");
}
【问题讨论】:
-
AFAIK,将
KeyAvailable设置回 false 是您实际上需要从流中读取密钥(例如,通过使用 this) -
我无法使用任何需要输入的方法。如果我这样做,它将完全停止并等待用户输入。该方法应该保持写入。所以如果我不能用 KeyAvailable 做到这一点,我有什么选择?
-
Console.ReadKey() 有点工作,但问题是我按住的键显示字符。
-
然后执行 Console.ReadKey(true)。
-
Console.ReadKey(true)应该导致字符不会被回显到窗口(这就是this 所说的)