What is the command for that ? I need to do a range of actions depending upon which key is pressed , so for ex. if key H is pressed the Help section is printed on screen .
What is the command for that ? I need to do a range of actions depending upon which key is pressed , so for ex. if key H is pressed the Help section is printed on screen .
Check the FAQ on how to get input without having the user press Enter.
Last edited by sea_4_ever; 08-27-2007 at 11:03 PM. Reason: "press" has two 's's in it. I forgot one.
Hmm, well it depends on your needs I would guess. If h is in the input it was definitely pressed. You can code an event-driven system quite easily. For example
Code:#include <stdio.h> #include <ctype.h> int dispatcher( const char job ) { switch( tolower( job ) ) { case 'a': puts( "Welcome to the Matrix!" ); /** call a function to print a welcome message. **/ fflush( stdout ); return 0; case 'b': return 0; /** Just exit. **/ default: break; /** This case does nothing. **/ } return job; } int main( void ) { char line[80]; do { puts( "Enter a for the red pill, b for the blue pill." ); fflush( stdout ); fgets( line, sizeof line, stdin ); } while( dispatcher( line[0] ) ); return 0; }
Hi citizan thanks for that. Sorry I forgot to mntion this but I was looking for something that runs in the background so to speak ,,, ie there is no input required from the user , it for ex. just displays help info when key h is pressed no matter what part of the program the user is in. Here is info what I received from someone else , but I am not sure how to use this in my situation :
Code:while(1) { if(kbhit()) { /* write your keybd traps here */ } //write your rest of the program here. }
Look into ncurses or PDCurses (additional ncurses support for windows).
If you're on windows, you can use GetAsyncKeyState() Or similar functions...
Has anyone ever wondered what would have happened if Neo had picked the blue pill?
A lot of inventions came from the Matrix movies, if they never happened, what state would the world be in now?
Maybe, the guys who made the Matrix would have chaneled their energy into something more "worthwhile" like a new FF game!
The world MIGHT have been a better place.
NVM it turns out you don't have to have it running in the background .