我正在try 用C语言编写一个程序(在Linux上),该程序循环直到用户按下一个键,但不需要按键来继续每个循环.
有没有简单的方法可以做到这一点?我想我可能用select()
块就能做到,但这似乎是一个很大的工作.
或者,有没有办法在程序关闭之前捕捉到一个ctrl-c键进行清理,而不是非非阻塞io?
我正在try 用C语言编写一个程序(在Linux上),该程序循环直到用户按下一个键,但不需要按键来继续每个循环.
有没有简单的方法可以做到这一点?我想我可能用select()
块就能做到,但这似乎是一个很大的工作.
或者,有没有办法在程序关闭之前捕捉到一个ctrl-c键进行清理,而不是非非阻塞io?
如前所述,您可以使用sigaction
来捕获ctrl-c,或者使用select
来捕获任何标准输入.
但是请注意,对于后一种方法,还需要设置TTY,使其处于每次字符模式,而不是每次行模式.后者是默认值——如果键入一行文本,则在按enter键之前,不会将其发送到正在运行的程序的stdin.
你需要使用tcsetattr()
功能来关闭开启模式,也可能会禁用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 */
}