Rob
Rob

Reputation: 718

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.

Upvotes: 5

Views: 3028

Answers (2)

William McBrine
William McBrine

Reputation: 2256

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

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

Upvotes: 8

Rob
Rob

Reputation: 718

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

Upvotes: 1

Related Questions