Getting user key input in command-line app

  • Thread starter Marc Scheuner [MVP ADSI]
  • Start date
M

Marc Scheuner [MVP ADSI]

Folks,

I have a command-line app that I'd like to show stuff onto the
Console. As a help to my users, I want to show the list of strings in
"pages", e.g. I'd like to pause output after x strings, and wait for
the user to either press "ENTER" to continue, or "ESC" to stop output.

Can't seem to get this to work in .NET ! .....

Here's my code fragment:

foreach(string sTemp in myArrayList)
{
ix++;
Console.WriteLine(ix.ToString("D3") + ": " + sTemp);

if(ix % 18 == 0)
{
Console.Write("---[ Any key to continue, Ctrl-C to
quit...]--- ");
int iKey = Console.Read();
if(iKey == -1)
{
Console.WriteLine();
return;
}
Console.WriteLine();
}
}

My first problem is that I can't seem to get just the next keypress
from the console - things like ESC and other keys are completely
swallowed, and never get detected.

With this solution, I can stop the output with a Ctrl-C - trouble is,
though - after the first 18 strings, the output jumps past the next 36
- it doesn't stop after string no. 36, but just goes on, and only
stops after string no. 54 again - from that point on, it also shows up
in two groups of 18 strings each.... don't quite understand why....

Any takers?? How can I trap keyboard input (I'd like to make it
possible that the user doesn't have to tap "ESC" and then "Enter" -
I'd like to be able to just detect "ESC" being pressed right away, and
abort. Or at least, I'd like to make this solution work so it'll
reliably always stop after my groups of 18 strings each.

Thanks!
Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
P

Patrick Steele [MVP]

Folks,

I have a command-line app that I'd like to show stuff onto the
Console. As a help to my users, I want to show the list of strings in
"pages", e.g. I'd like to pause output after x strings, and wait for
the user to either press "ENTER" to continue, or "ESC" to stop output.

Can't seem to get this to work in .NET ! .....

Here's my code fragment:

foreach(string sTemp in myArrayList)
{
ix++;
Console.WriteLine(ix.ToString("D3") + ": " + sTemp);

if(ix % 18 == 0)
{
Console.Write("---[ Any key to continue, Ctrl-C to
quit...]--- ");
int iKey = Console.Read();
if(iKey == -1)
{
Console.WriteLine();
return;
}
Console.WriteLine();
}
}

My first problem is that I can't seem to get just the next keypress
from the console - things like ESC and other keys are completely
swallowed, and never get detected.

With this solution, I can stop the output with a Ctrl-C - trouble is,
though - after the first 18 strings, the output jumps past the next 36
- it doesn't stop after string no. 36, but just goes on, and only
stops after string no. 54 again - from that point on, it also shows up
in two groups of 18 strings each.... don't quite understand why....

Any takers?? How can I trap keyboard input (I'd like to make it
possible that the user doesn't have to tap "ESC" and then "Enter" -
I'd like to be able to just detect "ESC" being pressed right away, and
abort. Or at least, I'd like to make this solution work so it'll
reliably always stop after my groups of 18 strings each.

You need to switch the console from the default 'line' mode to
'character' mode. See:

http://groups.google.com/groups?selm=#dVRo$mRBHA.2132@tkmsftngp05
 
M

Marc Scheuner [MVP ADSI]

You need to switch the console from the default 'line' mode to
'character' mode. See:

Thanks, this works nicely, but solves only half my problem - while now
I can do the paging okay (it reliably stops after my 18 strings every
time), I still cannot seem to deal with the "ESC" key being pressed -
I'm just not getting anything back from Console.Read() when I press
ESC...... how can I get that working??

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
P

Patrick Steele [MVP]

Thanks, this works nicely, but solves only half my problem - while now
I can do the paging okay (it reliably stops after my 18 strings every
time), I still cannot seem to deal with the "ESC" key being pressed -
I'm just not getting anything back from Console.Read() when I press
ESC...... how can I get that working??

Ick! You're right. I wonder what's so special about the ESC key??
Sorry, not sure what's happening here. I'll see if I can dig around and
find a cause.

Any chance you could pick a different key? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top