Search code examples
ccurses

What does the halfdelay function in curses do?


I am trying to make sense of the following C program :

#include <curses.h> 

int main() {
    int i; 
    initscr(); 
    halfdelay(5);
    for (i=0; i < 5; i++) 
        getch(); 
    endwin();
}

But I cannot make sense of it. I understand initscr() initialising the current screen, and that getch() is waiting for user input to unlock the current terminal, but what is the loop and halfdelay() accomplishing here ?


Solution

  • halfdelay(n); sets an input mode where the getch function waits n tenths of a second (in your example program, half a second) for the user to type something. getch returns the keypress, unless the timer elapses, in which case it returns ERR. This mode can be turned off again with cbreak() or nocbreak().

    This can be used in code that, e.g., asks the user for confirmation but defaults to some value if they don't respond within a certain time frame.