如前所述,您可以使用 sigaction 捕获 ctrl-c,或使用 select 捕获任何标准输入。
但是请注意,使用后一种方法,您还需要设置 TTY,使其处于一次字符而不是一次一行模式。后者是默认设置 - 如果您输入一行文本,它不会发送到正在运行的程序的标准输入,直到您按下回车键。
您需要使用tcsetattr() 函数来关闭 ICANON 模式,并且可能还要禁用 ECHO。从内存中,您还必须在程序退出时将终端设置回 ICANON 模式!
为了完整起见,这是我刚刚敲出的一些代码(nb:没有错误检查!),它设置了一个 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 */
}