我是mac的cpp新手。当我在我的程序中使用kbhit()时,我得到了错误。我使用了#include,但也得到了错误,所以我使用#include进行了搜索和测试,但错误仍然存在。所以请帮帮我。提前谢谢。
发布于 2008-11-23 06:10:43
不知道这是否能在Mac上工作,但这里有一些我用来在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 );
}发布于 2008-11-23 06:02:46
kbhit()是非标准的。事实上,我不相信有一个标准的函数来检测键盘输入。你能做的最好的事情就是使用例如fgetc从stdin中读取一个字符,并且希望它不会被从其他地方重定向。
https://stackoverflow.com/questions/312185
复制相似问题