nonblocking keyboard input?
does anybody know how to do nonblocking keyboard input for a console?
i'm trying to create a loop, but i don't want it to stop and wait for keyboard input if there isn't any. any clues? |
create a loop in what language?
|
c++
|
so your trying to make a program that outputs to the console, but for what reason would it stop and wait for a keyrequest? needz more detailz on tha program. example code would be nice.
|
void moveU( int &x,int &y, int &w, int &h, int &vx, int &vy, int &ph, int &ps) {
char move = ' '; do { for (int a=0; a<1000; a++); system("clear"); if ( move == 'k' && ps<(h-3) ) { ps += 1; } else if ( move == 'i' && ps>1) { ps -= 1; } drawScreen(h,w,x,y,ph,ps); cout << "ps is: " << ps << ", ph is: " << ph; cout << ", x is: " << x << ", y is: " << y << ", "; cout << "your last choice was: " << move << endl; move = 'a'; cout << "x to quit, i to move up, k to move down: "; cin >> move; } while ( move != 'x' ) ; } i want this function to be able to loop but not wait for keyboard input |
i copied your code and made a quick run of it and dont see any problem because you are requesting a key to be pressed right here "cin >> move". i didnt mess with drawScreen() since i dont have the code for it but from what i can tell it works fine.
your loop is your while(move != x) which is dependent on a key press. |
is there any way to change the code so it loops even if the user doesn't enter anything? like if they press a key it will take note of it and act on it, but otherwise it will just keep looping?
|
actualy i'm looking into it right now. rather interesting i think :)
i'll post something when i've figured it out. |
thanks for the help :D
|
the coolest thing to do is use SIGIO and signal driven i/o for stdin. it's quite simple with a couple calls to fcntl() to set the file owner and signal driven i/o flag and installation of a signal handler for SIGIO. you can go about and do w/e you want, and when the user hits the keyboard you'll run the signal handler and read from the keyboard. then you'll get returned back to where you were.
|
here i found this on my comp:
Code:
/* |
dang, thats a bit to go through. but much thanx! gona go through it in the morning. me sleepies.
Rebelcan, i'll check here in the morning to see if anybody has answered it or not, either way i'm still gona work on it. |
Just another idea....
You could use fork() and have one process handle the input from the user and have the other process drawing on the screen (or whatever you want to do). :study: read up on fork() |
Set the terminal to non-canonical mode, and use select() with a (small) timeout to read from stdin.
Or, see: http://www.linuxquestions.org/quest...p?postid=161647 |
Hko, the link you gave is dead. what did you type in for a search?
|
All times are GMT -5. The time now is 08:14 AM. |