GregoryComer
GregoryComer

Reputation: 830

How can I read multiple lines of user input from the console?

I want to read multiple lines of user input from the console, but also allow termination of input with an escape sequence like CTRL-C. My problem is canceling the last call to ReadLine() when the escape sequence is pressed. I've tried multithreading and forcing the thread to close, but my problem is getting the last line of user input. When the escape sequence is pressed, the last line is lost and I can't get it back using ReadLine() from the main thread. If I input something like this:

Input
Input2 [CTRL-C],

I only get 'Input\n'. Is there a way to get multiple lines of input, but capture the last line when the escape sequence is pressed?

Upvotes: 1

Views: 2617

Answers (1)

adv12
adv12

Reputation: 8551

As suggested in my comment above, it apparently works to:

  • set a console control handler for CTRL+C and
  • handle the CTRL-C by looping through the remaining input using Console.KeyAvailable and Console.ReadKey.

Upvotes: 3

Related Questions