如前所述,您可以使用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 */
}