Linux非阻塞键盘监听,C非阻塞键盘输入

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

请注意,使用后一种方法,您还需要设置TTY,以便它是一次一个字符,而不是一次一行模式。后者是默认的 – 如果你键入一行文本,它不会被发送到正在运行的程序的stdin,直到你按enter键。

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

只是为了完整性,这里有一些代码我刚刚敲了(nb:没有错误检查!),它设置一个Unix TTY并模拟DOS< conio.h>函数kbhit()和getch():

#include

#include

#include

#include

#include

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);

}

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 */

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值