Skip to content

Instantly share code, notes, and snippets.

@ideawu
Created October 19, 2016 08:25
Show Gist options
  • Save ideawu/8dbaa07e267218bbf7e45880d97e5a95 to your computer and use it in GitHub Desktop.
Save ideawu/8dbaa07e267218bbf7e45880d97e5a95 to your computer and use it in GitHub Desktop.
Linux Command Line Check Keyboard Any Press
#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