Immediate user input process (without pressing enter?)


3 replies [Last post]
Alder
Alder's picture
Offline
Joined: 2012-07-19
Points: 0

Is there any way to process user input (in the command prompt) as soon as the user presses a key?  For example, if I had a simple menu such as:

 

Select an option:

A - Addition

S - Subtraction

M - Multiplcation

 

And I wanted the program to proceed as soon as the user typed the 'a', 's', or 'm' character, rather than <character> [ENTER] ... is this possible?  

 

Another application I was considering would be similar to the Game Of Life application, but be able to control one of the characters and move its position (using the arrow keys if possible, or just wasd).  It would get repetitive to press enter to input every move.  Is there any way to avoid that?  Thanks.

 

EDIT: And while I'm on the subject, are the arrow keys recognized by the C language (if that makes sense)?  Is there a way to check if the user presses an arrow key (or other keys: home, end, pg up, etc.)?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Kevin
Offline
When things break I did it. I am an admin!Enjoy my soothing baritone.Completionist. I am better than you.My name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyDriving Ms. ChickyYou maniac! You blew it up! The compiler that is.Halfsies!W007! I watched 10 vids!Boot up or shut up!I don't know when to shut up.I'm not a fanboy!
Joined: 2011-03-20
Points: 2570

There are several ways to capture this. One way is to scan for interrupts like we did in an old WiBit.Net tutorial (Keystroke Logger in C++)
http://www.youtube.com/watch?v=rn20tjb1RDc
http://thepiratebay.se/torrent/4123532/

That works, but is considered "old school." Try to run it as a thread and see if it works for you.

I have also used the Microsoft getch function, which if you Google you can find a bunch of articles about it.

Alder
Alder's picture
Offline
Joined: 2012-07-19
Points: 0

Thanks, Kevin!  That definitely steered me in the right direction.

Kevin
Offline
When things break I did it. I am an admin!Enjoy my soothing baritone.Completionist. I am better than you.My name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyDriving Ms. ChickyYou maniac! You blew it up! The compiler that is.Halfsies!W007! I watched 10 vids!Boot up or shut up!I don't know when to shut up.I'm not a fanboy!
Joined: 2011-03-20
Points: 2570

Awesome! Glad to help!