C非阻塞键盘输入

标签 c linux asynchronous input nonblocking

我正在尝试用 C(在 Linux 上)编写一个程序,该程序循环直到用户按下某个键,但不需要按键来继续每个循环。

有没有简单的方法可以做到这一点?我想我可以用 select() 来做到这一点,但这似乎需要很多工作。

或者,有没有办法在程序关闭之前捕获 ctrl-c 按键来进行清理,而不是非阻塞 io?

最佳答案

如前所述,您可以使用 sigaction捕获 ctrl-c,或 select捕获任何标准输入。

但请注意,使用后一种方法时,您还需要设置 TTY,使其处于一次字符模式而不是一次一行模式。后者是默认设置 - 如果您输入一行文本,则在按 Enter 之前它不会发送到正在运行的程序的标准输入。

您需要使用tcsetattr()函数可以关闭 ICANON 模式,也可能禁用 ECHO。根据内存,当程序退出时,您还必须将终端设置回 ICANON 模式!

为了完整起见,这里有一些我刚刚敲出的代码(注意:没有错误检查!),它设置了 Unix TTY 并模拟 DOS <conio.h>功能kbhit()getch() :

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <termios.h>

struct termios orig_termios;

void reset_terminal_mode()
{
    tcsetattr(0, TCSANOW, &orig_termios);
}

void set_conio_terminal_mode()
{
    struct termios new_termios;

    /* take two copies - one for now, one for later */
    tcgetattr(0, &orig_termios);
    memcpy(&new_termios, &orig_termios, sizeof(new_termios));

    /* register cleanup handler, and set the new terminal mode */
    atexit(reset_terminal_mode);
    cfmakeraw(&new_termios);
    tcsetattr(0, TCSANOW, &new_termios);
}

int kbhit()
{
    struct timeval tv = { 0L, 0L };
    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(0, &fds);
    return select(1, &fds, NULL, NULL, &tv) > 0;
}

int getch()
{
    int r;
    unsigned char c;
    if ((r = read(0, &c, sizeof(c))) < 0) {
        return r;
    } else {
        return c;
    }
}

int main(int argc, char *argv[])
{
    set_conio_terminal_mode();

    while (!kbhit()) {
        /* do some work */
    }
    (void)getch(); /* consume the character */
}

关于C非阻塞键盘输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33271966/

相关文章:

c - 显示C—结构中所有汽车的最大气缸容量

javascript - AngularJS 异步将 href 分配给链接

c - 无法在 c 函数中返回正确的变量

c - 为什么 SIGINT 停止 sleep 不止一次?

c - 在c中获取物理硬盘列表

linux - Unix命令搜索文件中的一行并替换它

linux - 来自 Linux 的远程大型机 session

ios - 从 Swift 函数中的异步调用返回数据

javascript - 如何按顺序执行一组 Observable,仅在前一个 Observable 完成后才执行下一个?

c - 当我尝试使用 realloc 将新行插入 2D-Array 时出现段错误