问题
What is the difference between nodelay()
and cbreak()
in ncurses ? And why use these functions because we have getch()
! If i have understand getch()
permits to get the key directly just after the user hit the key. So nodelay
and cbreak
is useless isn't it ?
回答1:
They are two completely different functions.
From the documentation:
Normally, the tty driver buffers typed characters until a newline or carriage return is typed. The cbreak routine disables line buffering and erase/kill character-processing (interrupt and flow control characters are unaffected), making characters typed by the user immediately available to the program. The nocbreak routine returns the terminal to normal (cooked) mode.
...
The nodelay option causes getch to be a non-blocking call. If no input is ready, getch returns ERR. If disabled (bf is FALSE), getch waits until a key is pressed.
So cbreak
is if you want to disable line buffering, which you may want for many reasons. One is to avoid having to use fflush(stdout)
after each printout. With line buffering enabled, a printout will normally not be visible on screen until a newline character is printed.
The description of nodelay
is pretty self explanatory. getch
will not wait, but return instantly irregardless if any key is pressed or not.
来源:https://stackoverflow.com/questions/56245062/what-is-the-difference-between-nodelay-and-cbreak-in-ncurses