public async Task <ConsoleKeyInfo> ReadKeyAsync(CancellationToken cancellationToken) { await s_readKeyHandle.WaitAsync(cancellationToken); // I tried to replace this library with a call to `stty -echo`, but unfortunately // the library also sets up allowing backspace to trigger `Console.KeyAvailable`. InputEcho.Disable(); try { while (!await WaitForKeyAvailableAsync(cancellationToken)) { ; } } finally { InputEcho.Enable(); s_readKeyHandle.Release(); } await s_stdInHandle.WaitAsync(cancellationToken); try { return(System.Console.ReadKey(intercept: true)); } finally { s_stdInHandle.Release(); } }
private static async Task WaitForKeyAvailableAsync(CancellationToken cancellationToken) { InputEcho.Disable(); try { while (!Console.KeyAvailable) { await Task.Delay(50, cancellationToken); } } finally { InputEcho.Enable(); } }