0

When using Console.Read(), the implementation seems to think that as soon as you push enter, you've entered enough bytes for an eternity of reads. For example, if Read is called twice in a row, you can't enter one value on one line, push enter, and move to the next. Even if you only entered one character, Read just returns zero (Edit: Or one. I'm not too sure.). I've got this problem with ReadLine too. I'm trying to keep my console open for input after the program's terminated (I have a WPF app and used AllocConsole manually) and/or prompt the user for each individual slice of input. But it worketh not. Is there some button to ask it to block if there's no input available?

I wrote a Brainfuck interpreter, and the example programs from Wiki produce the intended result, if they don't use input.

What I want to do is enter one character, push enter, get that character as a char, repeat.

5
  • Can you share some code, Console.ReadLine should accept input until enter is pressed and then a subsequent Console.ReadLine will accept further input until enter is pressed. Commented Jul 3, 2010 at 14:08
  • But then I'd have a string. Read returns an int. Commented Jul 3, 2010 at 14:22
  • so you want to read one key at a time? If so Console.ReadKey might be what you are looking for? Commented Jul 3, 2010 at 14:23
  • @Chris Taylor: That's even worse, because the input terminates after two characters (no enter required!) and you can never alter what you're about to put in. Commented Jul 3, 2010 at 14:30
  • your last edit has made your requirement much clearer. Commented Jul 3, 2010 at 15:24

1 Answer 1

1

After your last edit I expect the piece of code below might provide either what you want or point you in the right direction.

public static int ReadLastKey()
{
  int lastKey = -1;
  for(;;)
  {
    ConsoleKeyInfo ki = Console.ReadKey();
    if (ki.Key != ConsoleKey.Enter)
    {
      lastKey = (int)ki.KeyChar;          
    }
    else
    {
      return lastKey;
    }
  }       
}

The function ReadLastKey will read key strokes and return the last key pressed when you press enter.

Of course if you do not want multiple key presses to be recorded you can remove the loop and just use Console.ReadKey twice, once to get the key press and then a second time to wait for the enter key. Or some permutation of one of these.

Here is a simple version of the function that will allow only one key press and then wait for the enter key to be pressed. Note this is very simplistic, you might want to handle other exit conditions etc.

public static int ReadLastKey()
{
  int lastKey = -1;
  ConsoleKeyInfo ki;

  // Read initial key press
  ki = Console.ReadKey();

  // If it is enter then return -1
  if (ki.Key == ConsoleKey.Enter) return lastKey;

  lastKey = (int)ki.KeyChar;

  // Wait for the user to press enter before returning the last key presss,
  // and do not display key the errant key presses.
  do
  {
    ki = Console.ReadKey(true);
  } while (ki.Key != ConsoleKey.Enter);

  return lastKey;      
}
Sign up to request clarification or add additional context in comments.

1 Comment

First function works well enough for my purposes- this is hardly a professional project. When I need something better, I may just use a C++/CLI wrapper on cout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.