如何阅读用户写的内容(不按Enter)-控制台C# [英] How to read what user wrote (without hitting enter) - console C#

查看:84
本文介绍了如何阅读用户写的内容(不按Enter)-控制台C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这个问题的标题可能有一些问题,但是我没有发现更好的方法,所以就这样:

Well, the caption of the question might be a little off, but I didn't figure any better, so here it goes:

我想读什么用户键入了到目前为止,我需要它像这样工作:用户输入一些必需的数据,然后询问他/她是想要完整的进度输出还是只是结果。

I would like to read what user typed 'so far', I need it to work like this: User inputs some required data and then is asked if he/she wants the full progress output or just the results.

当然,我可以打电话给ReadLine看看他/她写的是什么,但是我希望它更整洁-在输入最终数据后,他/她会被要求提供详细信息。该程序将等待2秒钟,然后读取用户编写的内容。如果没有,程序将正常运行,如果需要字符串,程序将提供详细的计算。

Of course, I could call ReadLine and see what he/she writes, but I'd like to have it neater - after the final data input, he/she would be asked for the detailed info. The program would wait like 2sec and then 'read' what the user wrote. If nothing, the program would run normally, if the required string, the program would provide detailed calculations.

我希望很清楚

感谢答案

E:另一种方式是在这2秒钟后模拟 enter 键,因此可以选择

E: Another way would be simulating a enter key press after those 2 seconds, so either this or that would be nice.

ANSWER
对于任何对工作代码感兴趣的人。

ANSWER For anyone interested in the working code.

bool timepassed = false;
bool detailmode = false;

long start = System.Environment.TickCount;

while (Console.KeyAvailable == false) // This loop will quit when KeyAvailable is true, i.e. a key is pressed (OR by the break command below)
{
    Console.Write("."); // write dot every 100ms to indicate progress
    System.Threading.Thread.Sleep(100);
    if (System.Environment.TickCount - start > 1250){
        timepassed=true; // if more than 1250ms passed, set flag break
        break;
    }
}

detailmode = !timepassed; // if time didnt pass, then user did press a key (and vice versa)->set detailmode
if (detailmode == true)
{
    Console.ReadKey(true); // hide the pressed key
    Console.WriteLine("\n\n-Detailni mod-"); // notify user about detailed mode
}


推荐答案

看看这两篇文章:

  • KeyDown event in console
  • KeyAvailable Property

特别是MSDN文章中的示例:

In particular, the MSDN article has this example:

class Sample 
{
    public static void Main() 
    {
    ConsoleKeyInfo cki = new ConsoleKeyInfo();

    do {
        Console.WriteLine("\nPress a key to display; press the 'x' key to quit.");

// Your code could perform some useful task in the following loop. However, 
// for the sake of this example we'll merely pause for a quarter second.

        while (Console.KeyAvailable == false)
            Thread.Sleep(250); // Loop until input is entered.
        cki = Console.ReadKey(true);
        Console.WriteLine("You pressed the '{0}' key.", cki.Key);
        } while(cki.Key != ConsoleKey.X);
    }
}

编辑-根据您实际上想对输入执行什么操作,可能希望将其存储在缓冲区或 StringBuilder 中。

EDIT - Depending on what you actually do with the input you might want to be accumulating it in a buffer or StringBuilder.

这篇关于如何阅读用户写的内容(不按Enter)-控制台C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆