Skip to content

Instantly share code, notes, and snippets.

@johnsyweb
Created February 25, 2012 21:29
Show Gist options
  • Save johnsyweb/1910950 to your computer and use it in GitHub Desktop.
Save johnsyweb/1910950 to your computer and use it in GitHub Desktop.
#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