获取在C#中输入控制台输入所需的时间

问题描述 投票:1回答:2

我有一个方法,使用Console.ReadLine()从控制台获取输入

我想知道用户写入输入的时间。

我知道我可以用StopWatch记录时间,但我只想记录第一个按键和回车键之间的时间。

我能做什么?

用谷歌翻译翻译

c# console stopwatch
2个回答
4
投票

版本1

Stopwatch stopWatch = new Stopwatch();

// Read first key pressed, then start stopwatch
var firstChar = Console.ReadKey();
stopWatch.Start();

// Read the rest followed by enter, then stop stopwatch
var restOfString = Console.ReadLine();
stopWatch.Stop();

// Join first char and rest of string together
var wholeString = string.Concat(firstChar.KeyChar, restOfString);
TimeSpan ts = stopWatch.Elapsed;

Console.WriteLine($"String entered: {wholeString}.");
Console.WriteLine($"It took {ts.Seconds} seconds.");
Console.ReadLine();

版本2

var keyInfo = new ConsoleKeyInfo();
var userInput = new StringBuilder();
var stopWatch = new Stopwatch();
var started = false;

do
{
     keyInfo = Console.ReadKey(false);

     if (started == false)
     {
          stopWatch.Start();
          started = true;
     }                

     switch (keyInfo.Key)
     {
          case ConsoleKey.Backspace:
              Console.Write(" \b");
              if(userInput.Length > 0) userInput.Remove(userInput.Length - 1, 1);
              break;
          // Stopping delete key outputting a space
          case ConsoleKey.Delete:
              Console.Write("\b");
              break;
          case ConsoleKey.Enter:
              break;
          default:
              userInput.Append(keyInfo.KeyChar);
              break;
    }
}
while (keyInfo.Key != ConsoleKey.Enter);

stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;

var finalString = userInput.ToString();

Console.WriteLine();

Console.WriteLine($"String entered: {finalString}.");
Console.WriteLine($"It took {ts.Seconds} seconds.");

Console.ReadLine();

您可能希望整理处理其他特殊字符,因为这只是解决您的特定问题。另请注意,您可以使用ReadKey(true)来拦截输入并停止任何输出。然后,您可以使用Console.Write()控制自己的输出。

版本3

这里给出的选项是一个拦截和控制输出的版本。这是我的偏好。

var keyInfo = new ConsoleKeyInfo();
var userInput = new StringBuilder();
var stopWatch = new Stopwatch();
var started = false;

do
{
    keyInfo = Console.ReadKey(true);

    if (started == false)
    {
        stopWatch.Start();
        started = true;
    }

    if (keyInfo.Key == ConsoleKey.Backspace)
    {
        Console.Write("\b \b");
        if(userInput.Length > 0) userInput.Remove(userInput.Length - 1, 1);
    }
    else if (keyInfo.Key == ConsoleKey.Enter)
    {
        // Do nothing
    }
    else if(Char.IsLetter(keyInfo.KeyChar) ||
            Char.IsDigit(keyInfo.KeyChar) ||
            Char.IsWhiteSpace(keyInfo.KeyChar) ||
            Char.IsPunctuation(keyInfo.KeyChar))

    {
        Console.Write(keyInfo.KeyChar);
        userInput.Append(keyInfo.KeyChar);
    }
} while (keyInfo.Key != ConsoleKey.Enter);

stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;

var finalString = userInput.ToString();

Console.WriteLine();

Console.WriteLine($"String entered: {finalString}.");
Console.WriteLine($"It took {ts.Seconds} seconds.");

Console.ReadLine();

1
投票

这样的事情怎么样:

while (!Console.KeyAvailable)
    Thread.Sleep(250);

var start = DateTime.Now;
var input = Console.ReadString();
var stop = DateTime.Now; 
© www.soinside.com 2019 - 2024. All rights reserved.