Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I interrupt (not using thread) console.readline when a key is pressed?

Tags:

c#

console

Let me explain, this is what I'm trying to achieve:

Enter name:
F4-Quit

is there a way to display the next line (F4-Quit) without readline waiting for user input before displaying it? the reason is I wanted to, at anytime quit, even though name has not been entered yet (cursor is waiting), this is very useful in many circumstances where someone during the process of entering information changes their mind and wants to quit or go back.

If that's not possible what would be the way around it?

thank you!

like image 638
PinoyDev Avatar asked Nov 01 '13 20:11

PinoyDev


3 Answers

Just write your own version of ReadLine(). Here's a TryReadLine() version in the pattern of .NET's TryParse():

    static bool TryReadLine(out string result) {
        var buf = new StringBuilder();
        for (; ; ) {
            var key = Console.ReadKey(true);
            if (key.Key == ConsoleKey.F4) {
                result = "";
                return false;
            }
            else if (key.Key == ConsoleKey.Enter) {
                result = buf.ToString();
                return true;
            }
            else if (key.Key == ConsoleKey.Backspace && buf.Length > 0) {
                buf.Remove(buf.Length - 1, 1);
                Console.Write("\b \b");
            }
            else if (key.KeyChar != 0) {
                buf.Append(key.KeyChar);
                Console.Write(key.KeyChar);
            }
        }
    }
like image 94
Hans Passant Avatar answered Oct 12 '22 03:10

Hans Passant


If that's not possible what would be the way around it?

The best way around this would be to use a GUI application. This would eliminate the restrictions placed on you by the console.

In general, if you want complex user input control, a UI is going to be better than a console.

If you must use a console application, you likely would need to use Console.ReadKey in a loop instead of Console.ReadLine, and check for the "escape" key stroke. As soon as a new line character has been entered, you can process your "text" command/input/etc.

like image 36
Reed Copsey Avatar answered Oct 12 '22 02:10

Reed Copsey


Here's the solution as Jeppe pointed out

 public static void Main()
    {
        ConsoleKeyInfo cki;

        Console.Clear();

        // Establish an event handler to process key press events.
         Console.CancelKeyPress += new ConsoleCancelEventHandler(myHandler);

        while (true) {
            Console.Write("Press any key, or 'X' to quit, or ");
            Console.WriteLine("CTRL+C to interrupt the read operation:");

            // Start a console read operation. Do not display the input.
            cki = Console.ReadKey(true);

            // this process can be skipped 
            // Console.WriteLine("  Key pressed: {0}\n", cki.Key);

            // Exit if the user pressed the 'X' key. 
            if (cki.Key == ConsoleKey.F4) break;
       }
}
protected static void myHandler(object sender, ConsoleCancelEventArgs args)
{
            args.Cancel = true;
            go_back_to_main();
 }
like image 3
PinoyDev Avatar answered Oct 12 '22 04:10

PinoyDev