Created
October 19, 2016 08:25
-
-
Save ideawu/8dbaa07e267218bbf7e45880d97e5a95 to your computer and use it in GitHub Desktop.
Linux Command Line Check Keyboard Any Press
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined(OS_LINUX) || defined(OS_MACOSX) | |
// Linux (POSIX) implementation of _kbhit(). | |
// Morgan McGuire, morgan@cs.brown.edu | |
static int _kbhit() { | |
static const int STDIN = 0; | |
static int initialized = 0; | |
int bytesWaiting; | |
if (!initialized) { | |
// Use termios to turn off line buffering | |
struct termios term; | |
tcgetattr(STDIN, &term); | |
term.c_lflag &= ~ICANON; | |
tcsetattr(STDIN, TCSANOW, &term); | |
setbuf(stdin, NULL); | |
initialized = 1; | |
} | |
ioctl(STDIN, FIONREAD, &bytesWaiting); | |
return bytesWaiting; | |
} | |
static char _getch(void) { | |
char c; | |
if (fread(&c, 1, 1, stdin) < 1) return 0; | |
return c; | |
} | |
#endif | |
static char get_keystroke(void) | |
{ | |
if (_kbhit()) { | |
char c = _getch(); | |
if (c >= 32) return c; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment