C# .NET Core Console.ReadKey(true) doesn't (always) intercept the key on Ubuntu

Viewed 1026

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.

1 Answers

It's not totally clear what you're trying to accomplish. Why not this instead?

class Program
{
    static void Main(string[] args)
    {
        while (Console.ReadKey(true).Key != ConsoleKey.Escape) {}
    }
}

This works for me on Ubuntu 18.04.

(Note that some characters may still be printed on the screen if you're mashing the keyboard while the application is initializing. It takes some time for the runtime to initialize and compile your code before input can be intercepted. Once the program is running, however, all inputs should be captured.)

Related