Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 389
Rob Avatar asked Jun 23 '14 12:06

Rob


People also ask

What is meant by keyboard buffer?

A keyboard buffer is a section of computer memory used to hold keystrokes before they are processed. Keyboard buffers have long been used in command-line processing. As a user enters a command, they see it echoed on their terminal and can edit it before it is processed by the computer.

Does keyboard have a buffer?

A keyboard buffer is a small area in the computer's memory (RAM) that is used to temporarily store the keystrokes from the keyboard before they are processed by the CPU.

What is the capacity of keyboard buffer?

The buffer is composed of 16 2-byte entries. It holds up to 16 keystrokes until they are read via the BIOS services through interrupt 22 (16 hex).


2 Answers

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

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

like image 107
William McBrine Avatar answered Sep 19 '22 00:09

William McBrine


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);
}
like image 20
Rob Avatar answered Sep 21 '22 00:09

Rob