Created
February 25, 2012 21:29
-
-
Save johnsyweb/1910950 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#if WINDOWS | |
#include <conio.h> | |
#else | |
#include <fcntl.h> | |
#include <termios.h> | |
#include <unistd.h> | |
class scoped_terminal_settings | |
{ | |
public: | |
scoped_terminal_settings() | |
:original_terminal_state() | |
,terminal_state() | |
,original_stdin_state(0) | |
{ | |
tcgetattr(STDIN_FILENO, &original_terminal_state); | |
terminal_state = original_terminal_state; | |
terminal_state.c_lflag &= ~(ICANON | ECHO); | |
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_state); | |
original_stdin_state = fcntl(STDIN_FILENO, F_GETFL, 0); | |
fcntl(STDIN_FILENO, F_SETFL, original_stdin_state | O_NONBLOCK); | |
} | |
~scoped_terminal_settings() | |
{ | |
tcsetattr(STDIN_FILENO, TCSANOW, &original_terminal_state); | |
fcntl(STDIN_FILENO, F_SETFL, original_stdin_state); | |
} | |
private: | |
termios original_terminal_state, terminal_state; | |
int original_stdin_state; | |
}; | |
bool kbhit() | |
{ | |
scoped_terminal_settings ts; | |
return EOF != getchar(); | |
} | |
#endif | |
void get_any_key() | |
{ | |
std::cout << "Press any key to continue..." << std::endl; | |
while (!kbhit()) | |
{ | |
sleep(0); | |
} | |
} | |
int main() | |
{ | |
get_any_key(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment