我正在研究我的第一个.NET核心应用程序,并且要尝试他们设置了Ubuntu VM的跨平台功能。安装.NET后,我将文件移至VM并启动了程序。基本上一切都很好 - 除了一个例外。
当我使用Console.ReadKey(true);
时,我应该获取按键,但控制台不应显示它。在Windows上可以正常工作,按下键,什么也没有显示。但是在Ubuntu上,控制台仍然显示我按下的键。
那么,我该如何防止这种情况?看来显示的字符随着我的程序的复杂性而增加,Console.ReadKey(true);
的调用之间的更多时间表示更多的字符。
我希望你能帮我,尼尔斯。
代码:
namespace test
{
class Program
{
static void Main(string[] args)
{
long lastrun = DateTime.Now.Ticks;
while (!PressedESC())
{
if (DateTime.Now.Ticks >= (lastrun + (TimeSpan.TicksPerSecond * 5)))
{
lastrun = DateTime.Now.Ticks;
// Here could be some dataprocessing but its not needed here...
// The problem already occures with this code.
}
}
}
private static bool PressedESC()
{
return Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape;
}
}
}
->仅显示了几个快速键入的炭
using System;
using System.Threading;
namespace test
{
class Program
{
static void Main(string[] args)
{
long lastrun = DateTime.Now.Ticks;
while (!PressedESC())
{
if (DateTime.Now.Ticks >= (lastrun + (TimeSpan.TicksPerSecond * 5)))
{
lastrun = DateTime.Now.Ticks;
// Here could be some dataprocessing but its not needed here...
// The problem already occures with this code.
}
Thread.Sleep(50);
}
}
private static bool PressedESC()
{
return Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape;
}
}
}
->显示了每个字符类型。
我尝试了什么:
-Ubuntu 18.04 LTS(桌面((VM(终端
-Ubuntu 18.04 LTS(桌面((VM(GNOME终端
-Ubuntu 18.04 LTS(服务器((VM(终端
-Ubuntu 18.04 lts(服务器((VM(PUTTY(来自Windows(
结果总是相同的。
尚不完全清楚您要完成的工作。为什么不这样做?
class Program
{
static void Main(string[] args)
{
while (Console.ReadKey(true).Key != ConsoleKey.Escape) {}
}
}
这对我在Ubuntu 18.04中有用。
(请注意,如果您在应用程序初始化时捣碎键盘,则可能仍在屏幕上打印某些字符。运行时需要一些时间来初始化和编译您的代码,然后才能截获输入。但是,正在运行所有输入。(