modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / coverages / getch.h
blobef4b6637384ea316aee56a033baa7288315305b6
1 // From http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1042856625&id=1043284385
2 // Edited by Hu Xuesong @ Thu Apr 28 CST 2011
4 #ifndef _GA_GETCH_H
5 #define _GA_GETCH_H
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
11 #include <stdio.h>
12 #include <termios.h>
13 #include <unistd.h>
15 static inline int mygetch ( void ) {
16 int ch;
17 struct termios oldt, newt;
19 tcgetattr( STDIN_FILENO, &oldt );
20 newt = oldt;
21 newt.c_lflag &= ~( ICANON | ECHO );
22 tcsetattr( STDIN_FILENO, TCSANOW, &newt );
23 ch = getchar();
24 tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
26 return ch;
29 static inline int pressAnyKey (void) {
30 if ( !isatty(STDIN_FILENO) )
31 return -2; // # define EOF (-1) in <stdio.h>
32 // other errno in /usr/include/asm-generic/errno-base.h
33 fputs("\nPress any key to continue ... ", stderr);
34 //return mygetch();
35 int ch = mygetch();
36 fputs("\n", stderr);
37 return ch;
40 #ifdef __cplusplus
42 #endif
44 #endif /* getch.h */