I'm working on my first .NET Core apps and to try their cross-platform functionality I've set up an Ubuntu VM. After installing .NET I moved my files to the VM and launched the program. Basically everything works fine - with one exception.
When I use Console.ReadKey(true); I should get the pressed key but the console shouldn't show it. On Windows it works fine, the Key is pressed and nothing is shown. But on Ubuntu the console still shows the keys I press.
So, how can I prevent this?
It looks like the character shown increase with the complexity of my program, more time between the calls of Console.ReadKey(true); means more chars shown.
I hope you can help me, Nils.
Code:
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;
}
}
}
-> Only a few rapidly typed chars are shown
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;
}
}
}
-> Every char I type is displayed.
What I've tried:
- Ubuntu 18.04 LTS (Desktop) (VM) Terminal
- Ubuntu 18.04 LTS (Desktop) (VM) Gnome Terminal
- Ubuntu 18.04 LTS (Server) (VM) Terminal
- Ubuntu 18.04 LTS (Server) (VM) PuTTY (from Windows)
Result is always the same.