문제
타사 라이브러리를 사용하지 않고 C 언어로 키보드 이벤트를 어떻게 감지 할 수 있습니까? 신호 처리를 사용해야합니까?
해결책
표준적인 방법은 없지만 이것들은 당신을 시작해야합니다.
Windows :
getch();
유닉스 :
W. Richard Stevens의 Unix 프로그래밍 책 에서이 코드를 사용하여 터미널을 원시 모드로 설정 한 다음 read ()를 사용하십시오.
static struct termios save_termios;
static int term_saved;
int tty_raw(int fd) { /* RAW! mode */
struct termios buf;
if (tcgetattr(fd, &save_termios) < 0) /* get the original state */
return -1;
buf = save_termios;
buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
/* echo off, canonical mode off, extended input
processing off, signal chars off */
buf.c_iflag &= ~(BRKINT | ICRNL | ISTRIP | IXON);
/* no SIGINT on BREAK, CR-toNL off, input parity
check off, don't strip the 8th bit on input,
ouput flow control off */
buf.c_cflag &= ~(CSIZE | PARENB);
/* clear size bits, parity checking off */
buf.c_cflag |= CS8;
/* set 8 bits/char */
buf.c_oflag &= ~(OPOST);
/* output processing off */
buf.c_cc[VMIN] = 1; /* 1 byte at a time */
buf.c_cc[VTIME] = 0; /* no timer on input */
if (tcsetattr(fd, TCSAFLUSH, &buf) < 0)
return -1;
term_saved = 1;
return 0;
}
int tty_reset(int fd) { /* set it to normal! */
if (term_saved)
if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0)
return -1;
return 0;
}
제휴하지 않습니다 StackOverflow