清除键盘缓冲区

Rob*_*Rob 5 c++ linux buffer ncurses getch

在函数中,boo()我按下一个键,然后doSthTimeConsuming()调用该函数。

现在我在doSthTimeConsuming(). 问题是键被缓冲并且在下一次迭代中boo()已经有一个输入。

我可以先清除或禁用键盘缓冲boo()吗?

void boo()
{
    while(1)
    {
        c = getch();

        switch(c)
        ...
        break;
    }
}

void doSthTimeConsuming()
{
    usleep(1000000);
}

int main()
{
    WINDOW* main_win = initscr();
        cbreak();
        noecho();
        keypad(main_win, TRUE);

    while(1)
    {
        boo();
        doSthTimeConsuming();
    }

    return 0;   
}
Run Code Online (Sandbox Code Playgroud)

EDIT: 我找到了一种解决方法,但我仍在寻找清除缓冲区的解决方案。

Wil*_*ine 8

有一个功能就是为了这个目的: flushinp()

http://pubs.opengroup.org/onlinepubs/007908799/xcurses/flushinp.html