Domanda

I'm attempting to work with the Console.ReadKey() function in order to intercept the user's keystrokes and rebuild what they are typing on the screen (as I'll need to clear the screen often, move the cursor often, and this seemed like the most full-proof method of making sure what they typed didn't vanish or appear at random points all over the screen.

My question is: Has anyone else ever experienced a 1 character "lag", for lack of a better term, when doing something similar? Say I want to type the word "This". When I press "T", nothing shows up, no matter how long I wait. When I press "h", the "T" appears. "i", the "h" appears. The letter I type will not appear until I hit another key, even if that key is the space bar. Does anyone have any suggestions for what I am doing wrong? I'm sure it has to do with how I am using Console.Readkey, I just don't see what alternative would work. I have attached a small and simple example of this below.

Thank you!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;

    namespace ConsoleApplication2

{
    class Program
    {

        private static string userInput = "";
        static ConsoleKeyInfo inf;
        static StringBuilder input = new StringBuilder();

        static void Main(string[] args)
        {
            Thread tickThread = new Thread(new ThreadStart(DrawScreen));
            Thread userThread = new Thread(new ThreadStart(UserEventHandler));
            tickThread.Start();
            Thread.Sleep(1);
            userThread.Start();
            Thread.Sleep(20000);
            tickThread.Abort();
            userThread.Abort();
        }


        private static void DrawScreen()
        {
            while (true)
            {
                Console.Clear();
                Console.SetCursorPosition(0, 0);
                Console.Write("> " + userInput);
                Thread.Sleep(300);
            }
        }


        private static void UserEventHandler()
        {
            inf = Console.ReadKey(true);

            while (true)
            {
                if (inf.Key != ConsoleKey.Enter)
                    input.Append(inf.KeyChar);
                else
                {
                    input = new StringBuilder();
                    userInput = "";
                }

                inf = Console.ReadKey(true);

                userInput = input.ToString();

            }
        }

    }
}
È stato utile?

Soluzione

It is because you have 2 times Console.ReadKey()

If you change your code into this

    private static void UserEventHandler()
    {
        while (true)
        {
            inf = Console.ReadKey(true);
            if (inf.Key != ConsoleKey.Enter)
                input.Append(inf.KeyChar);
            else
            {
                input = new StringBuilder();
                userInput = "";
            }
            userInput = input.ToString();
        }
    }

It does not lag. the second Console.ReadKey() is blocking in your code. I did not check if you need the parameter true to the readkey, that's for you to find out

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top