I have a class that accepts input via a TextReader and will allow the class to receive input from either the console or from a text box.

The following is a very basic 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 do I change the above to implement ReadKey() as the Read() method in the above example keeps accepting input until the Enter key has been pressed? I'd like to implement ReadKey() so that it only accepts a single keypress.

Thanks.

** EDIT **

To clarify things further, I am looking to implement ReadKey() without using Console.ReadKey(). I am not sure it's even possible since Google is turning up nothing so far.

没有正确的解决方案

其他提示

i used

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

to loop until i get an enter press

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top