我知道它与 struct termios 有关,但我无法通过阅读手册页来弄清楚。我的目标是,只要在键盘上按下一个按钮,它就会被读取而无需按回车键,并且它不会打印在我的程序的屏幕上。显然,这两者对于创建游戏都很重要。谢谢!
最佳答案
这是一个基于 this post 的例子:
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
int main(int argc, char** argv)
{
struct termios old, new;
tcgetattr(STDIN_FILENO, &old); // get current settings
new = old; // create a backup
new.c_lflag &= ~(ICANON | ECHO); // disable line buffering and feedback
tcsetattr(STDIN_FILENO, TCSANOW, &new); // set our new config
printf("Reading 5 characters without local echo...\n");
for (int i = 0; i < 5; i++)
{
char x = getchar();
printf("[%c] ", x);
}
printf("\nRestoring terminal config\n");
tcsetattr(STDIN_FILENO, TCSANOW, &old);
return 0;
}
关于c - 如何使用 IOCTL 系统调用来禁用按键回显和缓冲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32809983/