c - 如何使用 IOCTL 系统调用来禁用按键回显和缓冲?

标签 c

我知道它与 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/

相关文章:

c++ - 防止自动转换为整数

c - 如何在 C 中使用 [] 声明指向 const 字符数组的 Const 指针

c - 文件管理中的 Powershell 循环

c - 动态内存分配

c - sendto 失败 UNIX 域套接字

c - 使用 Eclipse 和 mingw 限制 fftw 的数组大小?

c - Linux C,打开一个存在的文件,EEXIST错误bug

c - 如何增加每个循环中具有全局指针的数组大小

c++ - 反向offsetof/通过offset获取元素名称

c - 指向指针数组的段错误和内存损坏