I'm making a test console application. This application runs a void task (I can't change this fact), and for it to remain opened I insert Console.ReadLine at end of Main method.
Is there any way to consume each key being pressed from other threads? I tried the following but call to Peek is blocking the thread.
loop = Task.Run(async () =>
{
    var input = Console.In;
    while (running)
    {
        int key = input.Peek(); // blocks here forever
        if (key == -1)
        {
            await Task.Delay(50);
        }
        else
        {
            input.Read();
            if ((ConsoleKey)key == ConsoleKey.Enter)
            {
                Completed?.Invoke();
            }
            else
            {
                OnKeyDown((ConsoleKey)key);
            }
            // todo how to intercept keyup?
        }
    }
});
This is the main method
static void Main(string[] args)
{
    GrpcEnvironment.SetLogger(new Grpc.Core.Logging.ConsoleLogger());
    //setup MagicOnion and option.
    var service = MagicOnionEngine.BuildServerServiceDefinition(isReturnExceptionStackTraceInErrorDetail: true);
    var server = new global::Grpc.Core.Server
    {
        Services = { service },
        Ports = { new ServerPort("localhost", 12345, ServerCredentials.Insecure) }
    };
    // launch gRPC Server.
    server.Start();
    // and wait.
    Console.ReadLine();
}
what I want is basically to have a keyboard key pressed event listener on another thread.
I also tried global keyboard hooks but that does not work for console application.
 
    