ncurses 中的 nodelay() 和 cbreak() 有什么区别? [英] What is the difference between nodelay() and cbreak() in ncurses?

查看:60
本文介绍了ncurses 中的 nodelay() 和 cbreak() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ncurses 中的 nodelay()cbreak() 有什么区别?为什么要使用这些函数,因为我们有 getch()!如果我了解 getch() 允许在用户按下键后直接获取密钥.所以 nodelaycbreak 没用了吧?

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 ?

推荐答案

它们是两个完全不同的功能.

They are two completely different functions.

来自文档:

通常,tty 驱动程序会缓冲输入的字符,直到输入换行符或回车符.cbreak 例程禁用行缓冲和擦除/终止字符处理(中断和流控制字符不受影响),使用户键入的字符立即可供程序使用.nocbreak 例程将终端返回到正常(熟)模式.

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.

...

nodelay 选项使 getch 成为非阻塞调用.如果没有准备好输入,getch 返回 ERR.如果禁用(bf 为 FALSE),getch 将等待直到按下某个键.

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.

所以 cbreak 是如果您想禁用行缓冲,您可能出于多种原因想要禁用行缓冲.一种是避免在每次打印输出后都必须使用 fflush(stdout).启用行缓冲后,在打印换行符之前,打印输出通常不会在屏幕上可见.

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.

nodelay 的描述是不言自明的.getch 不会等待,但无论是否按下任何键都会立即返回.

The description of nodelay is pretty self explanatory. getch will not wait, but return instantly irregardless if any key is pressed or not.

这篇关于ncurses 中的 nodelay() 和 cbreak() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆