hi ppl. I am new to cpp in mac. I got error when I used kbhit() in my program. I used #include but got error too, so I searched and test with #include but error is still remained. so plz help me. thanks in advance.
From stackoverflow
-
kbhit() is non-standard. In fact, I don't believe there is a standard function for detecting keyboard input. The best you can do is read a character from stdin using e.g. fgetc, and hope it's not redirected from somewhere else.
-
No idea if this would work on Mac, but here's some code that I've used to get a single keypress on Linux.
int mygetch() { char ch; int error; static struct termios Otty, Ntty; fflush(stdout); tcgetattr(0, &Otty); Ntty = Otty; Ntty.c_iflag = 0; /* input mode */ Ntty.c_oflag = 0; /* output mode */ Ntty.c_lflag &= ~ICANON; /* line settings */ #if 1 /* disable echoing the char as it is typed */ Ntty.c_lflag &= ~ECHO; /* disable echo */ #else /* enable echoing the char as it is typed */ Ntty.c_lflag |= ECHO; /* enable echo */ #endif Ntty.c_cc[VMIN] = CMIN; /* minimum chars to wait for */ Ntty.c_cc[VTIME] = CTIME; /* minimum wait time */ #if 1 /* * use this to flush the input buffer before blocking for new input */ #define FLAG TCSAFLUSH #else /* * use this to return a char from the current input buffer, or block if * no input is waiting. */ #define FLAG TCSANOW #endif if ((error = tcsetattr(0, FLAG, &Ntty)) == 0) { error = read(0, &ch, 1 ); /* get char from stdin */ error += tcsetattr(0, FLAG, &Otty); /* restore old settings */ } return (error == 1 ? (int) ch : -1 ); } -
See my answer to a related question at http://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input#448982
0 comments:
Post a Comment