2

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
}
Martin Melka
  • 7,177
  • 16
  • 79
  • 138
  • You're essentially looking for the `KeyDown` event, but within a console application. I've a feeling this is going to be very tricky to handle. – Yuck Dec 19 '11 at 16:15
  • You would have to use ReadChar instead of ReadLine. there are signifcant considerations you might want to consider before attempting to do this. ReadChar reads in a single character, you would have to handle the return character, not exactly sure the reason you think it would be "neater" – Security Hound Dec 19 '11 at 16:16
  • sounds like a homework assignment to me..?? – MethodMan Dec 19 '11 at 16:16
  • Yes, this is console, thanks for adding the tag for me. @Yuck I thought so, but was hoping for some elegant out-of-the-box solution :) – Martin Melka Dec 19 '11 at 16:16
  • 1
    You'll have to implement Console.ReadLine() yourself with Console.ReadKey(). Example is here: http://stackoverflow.com/a/4805314/17034 – Hans Passant Dec 19 '11 at 16:17
  • 3
    @DJKRAZE To be honest, it is a homework, but completely unrelated. The homework is to count a definite integral (?) using various methods, this is just a little feature (which doesn't even have to be there) I want to add. I don't want to give up just because I can't figure it out quickly and it's bugging me off :) – Martin Melka Dec 19 '11 at 16:18
  • no problem... just trying to get a gist of what you are trying to do.. no disrespect intended.. – MethodMan Dec 19 '11 at 16:23
  • @DJKRAZE no problem here either :] – Martin Melka Dec 19 '11 at 16:29

3 Answers3

4

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.

Community
  • 1
  • 1
mavnn
  • 9,101
  • 4
  • 34
  • 52
  • I think ReadKey doesn't cut it. If the user doesn't press anything at all, after 2 secs I want the program to move on. But ReadKey would keep waiting for a keypress. or? – Martin Melka Dec 19 '11 at 16:22
  • @Magicmaster That's what the `while` loop is for in the example from MSDN. You do use `ReadKey`, but you need someway to keep the application alive while waiting for additional key presses. – Yuck Dec 19 '11 at 16:23
  • Added an amendment about dealing with time outs; I'd misunderstood your question originally. Head on over there and give them some up votes... – mavnn Dec 19 '11 at 16:52
4

Take a look at these two articles:

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.

Yuck
  • 49,664
  • 13
  • 105
  • 135
1
ConsoleKeyInfo cki = Console.ReadKey();
Console.WriteLine(cki.Key.ToString());
Andrea Colleoni
  • 5,919
  • 3
  • 30
  • 49