#include <termios.h>#include <stdio.h>static struct termios old, current;void initTermios(int echo) { tcgetattr(0, &old); current = old; current.c_lflag &= ~ICANON; if (echo) { current.c_lflag |= ECHO; } else { current.c_lflag &= ~ECHO; } tcsetattr(0, TCSANOW, ¤t); }void resetTermios(void) { tcsetattr(0, TCSANOW, &old);}char getch_(int echo) { char ch; initTermios(echo); ch = getchar(); resetTermios(); return ch;}char getch(void) { return getch_(0);}char getche(void) { return getch_(1);}int main(void) { char c; printf("(getche example) please type a letter: "); c = getche(); printf("nYou typed: %cn", c); printf("(getch example) please type a letter..."); c = getch(); printf("nYou typed: %cn", c); return 0;}输出:
(getche example) please type a letter: gYou typed: g(getch example) please type a letter...You typed: g



