0

I am trying to write a console program similar to "less", where I can pipe data in to it from the command line, and then interact with that data via the keyboard.

I am running in to the following exception: Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.

The suggested Console.Read appears to be reading from the pipe instead of the console, so that doesn't work for me.

I understand that I can not Console.ReadKey while Console.IsInputRedirected, but I believe there has to be some work around because programs such as less accomplishes the same goal.

Does anyone know how I can Console.ReadKey (or equivalent) when the standard input has been redirected?

I've already looked in to this article, but it doesn't appear to work. The call to AttachConsole returns false, and the console application terminates.

  • 1
    @Babak: this isn't a duplicate of that post. The issue here is that the OP wants to read stdin as the source for the text to display, but _also_ handle keyboard input, to control the display of that text. – Peter Duniho Feb 22 '17 at 08:29
  • @Michael: your question is way too broad. The basic answer is to not use any `Console` method that relies on stdin, for user input. Instead, you need to access keyboard input directly. But there's lots of references online explaining various techniques for doing that. You need to do the research to see how your console program can get keyboard input without using stdin. If you have difficulties even after that, post a new question with a good [mcve] showing clearly what you've tried, with an explanation of what _specific_ problem you still can't solve. – Peter Duniho Feb 22 '17 at 08:31
  • Thank you @PeterDuniho, I'll do a more research on capturing the key presses outside of the console. – Michael Miller Feb 22 '17 at 21:48
  • 1
    In case anyone else runs in to this problem, I was able to solve it using https://gist.github.com/Ciantic/471698 Thanks @PeterDuniho for pointing me in the right direction. – Michael Miller Feb 22 '17 at 23:13

0 Answers0