1

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user123
  • 8,970
  • 2
  • 31
  • 52

1 Answers1

3

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();
        }
    }
}
Phil
  • 2,315
  • 2
  • 18
  • 26
  • I really appreciate the effort and it seems to work correctly, but when I attempt to erase characters, the console would only change the position at which I'm typing without actually removing the text. edit: Then again, I might be able to check if the backspace button is clicked, clear the screen and redisplay the characters, but I feel that's a terrible thing to do, especially when there's a lot of text in the console. – user123 Jul 12 '12 at 14:44
  • Oh, you updated it. I wanted to respond to my own question, but it seems that I have an insufficient amount of reputation. I have the solution. I need to check if there are any characters in the input stream using Console.KeyAvailable. – user123 Jul 12 '12 at 14:54
  • I've updated it to use StringBuilder to record the input, and then count the words using Regex at the end. Hope this helps :) – Phil Jul 12 '12 at 15:05