Function Wait for an input without blocking the loops?
Aug 6, 2015 at 2:29pm UTC
i am using Mac os X, which running Unix system. i had tried getch() function which can't work in this case. I am wondering is that any method to make the function wait for an input without blocking? Thank you
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
#include <curses.h>
// checks if a key a has been pressed(non-blocking)
int kbhit()
{
//nodelay(stdscr, TRUE);
int ch;
getchar();
//ch = 1;
//system("stty cbreak -echo");
if (ch != ERR) {
//ungetch(ch);
return 1;
} else {
return 0;
}
}
using namespace std;
int main()
{
bool gameover = false ;
//system("stty raw");
while (!gameover)
{
if (kbhit())
{
cout << "You enter something" << endl;
gameover = true ;
}
else
{
cout << "nothing is enter\n" ;
gameover = false ;
}
}
//system ("stty cooked");
//system("stty cooked echo");
return 0;
}
Topic archived. No new replies allowed.