[英]Writing my own version of kbhit using <ncurses.h> for linux
bool kbhit(void)
{
// function without the 4 lines of code below will not work
// but they also screw up the console
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
int ch = getch();
if (ch != ERR) {
ungetch(ch);
refresh();
return true;
} else {
refresh();
return false;
}
}
Since ncurses do not have kbhit, I found the function above.由于ncurses没有kbhit,我找到了上面的函数。 But they screw up the console.
但是他们搞砸了控制台。 This is what happens when I try to print the follow: See picture
当我尝试打印以下内容时会发生这种情况:见图片
That's called "staircasing".这就是所谓的“阶梯”。
The example of kbhit
starts curses, but does not exit from curses (using endwin
). kbhit
的例子启动了curses,但不退出curses(使用endwin
)。 If your program tries using non-curses calls such as printf
, the terminal will still be in raw mode — and that will persist after exiting to the shell (unless something resets the terminal).如果您的程序尝试使用诸如
printf
类的非诅咒调用,则终端仍将处于原始模式——并且在退出到 shell 后仍将持续(除非某些东西重置了终端)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.