简体   繁体   English

使用编写我自己的 kbhit 版本<ncurses.h>用于 linux

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM