C 禁用命令行输入

标签 c command-line stdin ncurses appkit

我正在创建一个 ncurses 程序,它使用 AppKit 读取系统范围的击键。为了清除运行程序时在命令行上累积的文本墙,我在退出程序之前执行这行代码。

while((c = getch()) != '\n' && c != EOF) {}

我的问题是是否有更有效的方法来解决这个问题。例如,在程序执行时禁用命令行输入。

编辑:

我进行了一些测试,我的问题似乎根源于 usleep,而不是 ncurses 或 AppKit。这是一个例子:

#include <unistd.h>

int main() {
    usleep(5000000);
    return 0;
}

最佳答案

exit() 之前尝试 tcflush(STDIN_FILENO, TCIFLUSH)


根据tcflush(3):

int tcflush(int fd, int queue_selector);

tcflush() discards data written to the object referred to by fd but not transmitted, or data received but not read, depending on the value of queue_selector:

TCIFLUSH : flushes data received but not read.
TCOFLUSH : flushes data written but not transmitted.
TCIOFLUSH: flushes both data received but not read, and data written but not transmitted.

关于C 禁用命令行输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41551327/

相关文章:

c - stdin 和 stdout 实际上是同一个文件吗?

谁能告诉我为什么我的排序不起作用? (c代码)

c - Typedef 到双整数数组

mysql - 如何让 vi 键绑定(bind)在 mysql 客户端中工作?

java - 如何使用java从Json文件导入数据到Mongodb

c - fgets 之后获取键盘输入

c++ - 在终端应用程序中使用 opengl ( 没有 glut/glew )

c - 使用 fork 和 pipe 实现管道

unix - 如何从命令行找出计算机的体系结构?

ffmpeg - 如何通过管道将文件导入到 Whisper.cpp 中?