linux下实现类似windows的getch函数功能的代码:
##################代码开始##################
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
int getch() {
struct termios tm, tm_old;
int fd = , c;
if (tcgetattr(fd, &tm) < 0) {
return -1;
}
tm_old = tm; // save original mode
cfmakeraw(&tm);
if (tcsetattr(fd, TCSANOW, &tm) < 0) { // set new mode
return -1;
}
c = fgetc(stdin);
if (tcsetattr(fd, TCSANOW, &tm_old) < 0) { // set old mode
return -1;
}
return c;
}
int main() {
int c;
printf("getch function\n");
c = getch();
return 0;
}
##################代码结束##################