Question

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 ?

Était-ce utile?

La 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.

Autres conseils

halfdelay is used to disable buffering of chars with 50 sec check for user inactivity.

this sample reads 5 chars from user input. if user is inactive for 50 seconds then getch returns ERR with errno set to EINTR.

see details there and there

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top