Domanda

First of all, Console.ReadKey() is not the answer.

I need to be able to erase the first character I write.

Basically, what I'm trying to do is an application that measures your typing speed. It works perfectly, but I'm trying to work out the aesthetics.

What I was doing at first was calling Console.ReadKey(), then calling this function that starts a Timer (well, I realized 30 minutes after I wrote the application that there happens to be a Stopwatch class ._.), and I would store the string that the user inputs with Console.ReadLine(), then stop this Timer.

static Word[] getWords()
{
    Word[] words;
    int c = Console.ReadKey().KeyChar;
    Timing.start();
    string str = (c.ToString() + Console.ReadLine()).ToLower();
    charCount = str.Length;
    words = Word.process(str);
    Timing.stop();
    return words;
}

charCount is a static int, and Word is just a class that wraps a string. Word.process is a function that takes a string, omits all the symbols and returns an array of the words that the user types. Timing is just a class that wraps the Timer class. I think that's all that needs to be explained concerning the code.

What I need to do is call Timing.start() when the user types a character and he has to be able to erase it.

È stato utile?

Soluzione

Needs a bit of tweaking, but how about something like this?

Update - now one (1) backspace works, but multiple doesn't. argh! Hopefully this will point you in the right direction though.

Update #2 - using StringBuilder.... backspaces work now :)

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {

            StringBuilder sb = new StringBuilder();
            // you have alot of control on cursor position using
            // Console.SetCursorPosition(0, Console.CursorTop -1);
            List<DateTime> inputs = new List<DateTime>();
            ConsoleKeyInfo cki;

            Console.WriteLine("Start Typing...");
            Console.WriteLine("Press the Escape (Esc) key to quit: \n");
            do
            {
                cki = Console.ReadKey();
                if (cki.Key == ConsoleKey.Spacebar)
                {
                    sb.Append(cki.KeyChar);
                }

                else if (cki.Key == ConsoleKey.Backspace)
                {
                    Console.Write(" ");
                    Console.Write("\b");
                    sb.Remove(sb.Length - 1, 1);
                }

                else if (cki.Key == ConsoleKey.Enter)
                {
                    sb.Append(cki.KeyChar + " ");
                    Console.WriteLine("");
                }

                else
                {
                    sb.Append(cki.KeyChar);
                }

                inputs.Add(DateTime.Now);
            } while (cki.Key != ConsoleKey.Escape);

            Console.WriteLine("");
            Console.WriteLine("=====================");
            Console.WriteLine("Word count: " + Regex.Matches(sb.ToString(), @"[A-Za-z0-9]+").Count);

            TimeSpan duration = inputs[inputs.Count - 1] - inputs[0];

            Console.WriteLine("Duration (secs): " + duration.Seconds);
            Console.ReadLine();
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top