5

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.

Rob
  • 708
  • 8
  • 27
  • @bits_international It doesn't work. I think that condition c=='\n' OR c==EOF makes no sense here. Could you explain it? – Rob Jun 23 '14 at 12:58
  • I meant in this context. You can clearly see that i am using `cbreak();`. How does your code help? It doesn't work. – Rob Jun 23 '14 at 13:16
  • In this response you can find a portable way to clear the buffer: https://stackoverflow.com/a/13987787/1817986 – David Jun 12 '18 at 07:10

2 Answers2

8

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

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

William McBrine
  • 2,166
  • 11
  • 7
1

I resolve the problem using keypad(main_win, FALSE); after input is received and enabling it keypad(main_win, TRUE); when it's needed.

void boo()
{
    keypad(main_win, TRUE);
    while(1)
    {
        c = getch();

        switch(c)
        ...
        break;
    }
    keypad(main_win, FALSE);
}
Rob
  • 708
  • 8
  • 27