Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the halfdelay function in curses do?

Tags:

c

curses

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 ?

like image 422
user476033 Avatar asked Sep 07 '25 17:09

user476033


1 Answers

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.

like image 122
Fred Foo Avatar answered Sep 10 '25 07:09

Fred Foo