user1875195
user1875195

Reputation: 988

Check and get user input from terminal C#

I am trying to get user input from the terminal in C#, but I only want to ReadLine when the user starts typing. Basically the way I have it set up now is that I am in a while loop and I want to either check to see if I got messages or send them, but I don't want to get stuck trying to send one by calling ReadLine and waiting for the user to send a message if they have nothing to send at the moment. My code looks something like this:

While (true)
{
    // If messages to be received
        // Receive them

    // Check to see if the user is typing input
    if (Console.KeyAvailable)
    {
        string userInput = Console.ReadLine();
        // Do stuff...
    }   

}

Basically what is happening is that it works completely fine, but the first letter that the user types does not show up in the terminal window, but it does get picked up by the ReadLine no problem. Is there anyway I can get this to work so that the user can see everything they are typing right away?

Thanks!

Upvotes: 1

Views: 793

Answers (1)

Saeed Amiri
Saeed Amiri

Reputation: 22555

You can use Console.ReadKey(true); to read missing item, then read other parts with readline method append it to your input.

Upvotes: 1

Related Questions