What is the difference between halfdelay() and cbreak() + timeout() when getting input?

Viewed 160

I'm writing a typing game with ncurses in C++, and I noticed there are cbreak() and halfdelay() functions to switch modes for text input in ncurses.

I know cbreak() is used to disable line buffering when reading characters, and the difference between cbreak() and halfdelay() is halfdelay() take a parameter to blocking for X tenths.

And I was curious what if I use cbreak() with timeout(), does it works like halfdelay() ?

I made an experiment to see their difference:

Use halfdelay():

#include <ncurses.h>
int main()
{
    initscr();
    halfdelay(10); // blocking for 1s
    getch();
    endwin();
}

Use cbreak() and timeout():

#include <ncurses.h>
int main()
{
    initscr();
    cbreak();
    timeout(1000); // blocking for 1s
    getch();
    endwin();
}

I found both of behavior are waiting for a char and exit the programs immediately until exceed 1s, so I would like to ask if they have difference in ncurses?

1 Answers

The halfdelay routine is used for half-delay mode. It's very similar to cbreak, but after blocking for tenths tenths of seconds, ERR (Error stream) is returned if nothing has been typed.

You can find more informations about it here : Link

Related