Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Readline() and ReadKey() Simultaneously

Is there any way to detect both Readline and ReadKey, so that in most cases it behaves as a readline, except for some special key inputs that should be detected?

I need some "parallel" implementation to introduce simultaneity. The code below is synchronous and does not meet my need

while ((line = Console.ReadLine()) != "x")
{    
    if (line == "BLABLA")
    {
        //Stuff
    }

    else
    {
        //Stuff
    }

    ConsoleKeyInfo ki = Console.ReadKey(true);
    if ((ki.Key == ConsoleKey.V) && (ki.Modifiers == ConsoleModifiers.Control))
    {
        //Stuff
    }
}
like image 231
Mehdi LAMRANI Avatar asked Jan 09 '12 14:01

Mehdi LAMRANI


People also ask

What is the difference between ReadKey and ReadLine?

Readline() - Accept the string and return Integer. ReadKey() - Accept the character and return Character.

What does the line console ReadKey () do?

ReadKey() Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.

What type of data does console ReadLine () always return?

Return Value: It returns the next line of characters of string type from the input stream, or null if no more lines are available.

What is console ReadKey () in C#?

ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed. In short, it obtains the next character or any key pressed by the user.


1 Answers

Here's a function I just made to do this.

Right now it only handles Backspace, Enter and Esc, but it could easily be modified to handle other keys if you deem them necessary.

    // returns null if user pressed Escape, or the contents of the line if they pressed Enter.
    private static string ReadLineOrEsc()
    {
        string retString = "";

        int curIndex = 0;
        do
        {
            ConsoleKeyInfo readKeyResult = Console.ReadKey(true);

            // handle Esc
            if (readKeyResult.Key == ConsoleKey.Escape)
            {
                Console.WriteLine();
                return null;
            }

            // handle Enter
            if (readKeyResult.Key == ConsoleKey.Enter)
            {
                Console.WriteLine();
                return retString;
            }

            // handle backspace
            if (readKeyResult.Key == ConsoleKey.Backspace)
            {
                if (curIndex > 0)
                {
                    retString = retString.Remove(retString.Length - 1);
                    Console.Write(readKeyResult.KeyChar);
                    Console.Write(' ');
                    Console.Write(readKeyResult.KeyChar);
                    curIndex--;
                }
            }
            else
            // handle all other keypresses
            {
                retString += readKeyResult.KeyChar;
                Console.Write(readKeyResult.KeyChar);
                curIndex++;
            }
        }
        while (true);
    }
like image 124
Overlord Zurg Avatar answered Oct 25 '22 01:10

Overlord Zurg