Question

At the beginning of launching process of my program, there is a kind of short intro, in which chars are moving, box is slowly appearing, etc. etc. After that, the proper function is activating and it is waiting for user's input (getstr(prompt);). However, if I press any key while the intro is loading, the input is automaticaly conveyed to prompt string, which is not desirable. How to turn off reading from input till just a line before getstr(prompt); and then activate it? Or maybe there's a different way to approach this problem? My idea was to use a blocking function like this (not sure it would even work):

timeout (1);
while (intro == 1)
{
   continue;
}
timeout (-1);

But I think that checking this argument all the time is not an elegant way of dealing with problem.

Was it helpful?

Solution

I think the answer you want is flushinp().

From the manual page

The flushinp routine throws away any typeahead that has been typed by the user and has not yet been read by the program.

https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man3/flushinp.3x.html

Here is an example that might fit your usage

#include <stdlib.h>
#include <curses.h>

void atexit_cb(void) {
    endwin();
}

int main(void)
{
    // initialize curses
    initscr();
    raw();
    keypad(stdscr, TRUE);
    noecho();
    nodelay(stdscr, FALSE);
    atexit(atexit_cb);

    // show an intro for 3 seconds
    for (int i = 0; i < 3; i++) {
        mvprintw(i, 0, "...Intro text...");
        refresh();
        napms(1000);
        move(i, 0);
        clrtoeol();
    }

    // flush typeahead
    flushinp();

    // now get some new input
    printw("Press a key...");
    echo();
    getch();
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top