Embedding ReadKey () in TextReader

I have a class that accepts input via TextReader and allows the class to receive input from either the console or a text field.

Below is a very simple example of what I'm trying to do:

using System.IO; class TestReader() { public TextReader CurrentStream { get; } public void SetInputStream( TextReader reader) { Reader = reader; } } class Program { static void Main( string[] args ) { TestReader rdr = new TestReader(); rdr.SetInputStream(Console.In); var input = (char)rdr.CurrentStream.Read(); Console.WriteLine( "You selected: " + input ); } } 

How to change the above to implement ReadKey() , since the Read() method in the above example continues to accept input until the Enter key is pressed? I would like to implement ReadKey () so that it only accepts one keystroke.

Thanks.

** EDIT **

To clarify the situation, I am looking to implement ReadKey () without using Console.ReadKey (). I’m not sure if this is possible because Google is not doing anything yet.

+4
source share
1 answer

I used

  ConsoleKeyInfo info; do { info = Console.ReadKey(); } while (info.KeyChar != '\n' && info.KeyChar != '\r'); 

until i get input click

if you only have an input stream or textBox , you can just do an if . in case of console use my example, in case of textBox read its text

+1
source

Source: https://habr.com/ru/post/1497243/


All Articles