How to read what user wrote (without hitting enter) - console C#

拟墨画扇 提交于 2020-03-16 07:03:53

问题


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.

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.

I hope it's clear

Thanks for answers

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

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
}

回答1:


Take a look at these two articles:

  • KeyDown event in console
  • KeyAvailable Property

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);
    }
}

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




回答2:


I suspect you're looking for the ReadKey method:

http://msdn.microsoft.com/en-us/library/system.console.readkey.aspx

You'll need to handle every keypress in turn and rebuild into the overall command yourself at the end when the user finally presses enter.

Edit:

If you're looking to time out after a set period, check out the solutions in this question: How to add a Timeout to Console.ReadLine()?

I had misunderstood your intent originally.




回答3:


ConsoleKeyInfo cki = Console.ReadKey();
Console.WriteLine(cki.Key.ToString());


来源:https://stackoverflow.com/questions/8564090/how-to-read-what-user-wrote-without-hitting-enter-console-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!