Search code examples
c++linuxbufferncursesgetch

Clear buffer for keyboard


In function boo() I press a key, then the function doSthTimeConsuming() is called.

Now I am pressing keys during doSthTimeConsuming(). Problem is that keys are buffered and in the next iteration boo() will already have an input.

Could I clear or disable buffering for the keyboard in boo() first?

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;   
}

EDIT: I found a workaround but i am still looking for solution with clearing the buffer.


Solution

  • There is a function for this very purpose: flushinp()

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