本文介绍了Mac中的kbhit()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Mac中cpp的新手.在程序中使用kbhit()时出现错误.我使用了#include但也出现了错误,因此我使用#include进行了搜索和测试,但错误仍然存在.所以请帮帮我.预先感谢.
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.
推荐答案
不知道这是否可以在Mac上运行,但这是我用来在Linux上进行一次按键操作的一些代码.
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 );
}
这篇关于Mac中的kbhit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!