【问题标题】:Get the time taken to enter console input in C#获取在 C# 中输入控制台输入所花费的时间
【发布时间】:2018-07-27 18:50:08
【问题描述】:

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

我想知道用户写输入花了多长时间。

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

我能做什么?

用谷歌翻译器翻译

【问题讨论】:

  • 这样做的目的是什么?某种“输入密码需要多长时间”?
  • 可以,但不是密码
  • 我想得到每个单词的平均打字时间。
  • 我觉得你需要使用Console.ReadKey

标签: c# console stopwatch


【解决方案1】:

版本 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();

【讨论】:

  • 问题是我不能删除输入的第一个字符
  • 你不需要。想象一下它是 ReadLine 的主要输入字符串的一部分。我已将其拆分为一种查找第一次按键时间的方法。
  • 但是如果我想在写句子的同时删除字符怎么办?
  • 好吧,第 2 版将满足您的需求。
  • 还有一个版本 3 显示截取输出,这样您就可以更轻松地控制输出和添加到字符串的字符。
【解决方案2】:

这样的事情怎么样:

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

var start = DateTime.Now;
var input = Console.ReadString();
var stop = DateTime.Now; 

【讨论】:

  • 你的意思是ReadLine()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-08
相关资源
最近更新 更多