Source Code File 08-03_keywait2

08-03_keywait2.c

#include <ncurses.h>
   
int main()
{
    int value = 0;
    
    initscr();

    addstr("Press any key to begin:\n");
    refresh();
    getch();
    
/* turn off getch() wait */
    nodelay(stdscr,TRUE);
    addstr("Press the Spacebar to stop the loop!\n");
    while(getch() != ' ')
    {
        printw("%d\r",value++);
        refresh();
    }   
    
    endwin();   
    return(0);
}

Output Screenshot

Notes

* The difference between this code and 08-02_keywait1.c are in Line 15 and 16. The addstr() statement is specific here (as opposed to "any key"), and the comparison in the while loop is also specific.

* If you see the "any key" you press appear, and you don't want that key to appear, use the noecho() function before you use getch() to prompt for input.