# include <termios.h>
# include <unistd.h>
# include <cstdlib>
# include <errno.h>
static termios old_tios;
bool raw_stdin() noexcept {
/* Copy defaults from the old state. */
termios newtermios = old_tios;
newtermios.c_lflag &= ~(IEXTEN | ICANON);
newtermios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
newtermios.c_oflag &= ~OPOST; /* No post-processing. */
newtermios.c_cflag &= ~(CSIZE | PARENB);
/* One byte at a time */
newtermios.c_cc[VMIN] = 1; newtermios.c_cc[VTIME] = 0;
return (tcsetattr(STDIN_FILENO, TCSAFLUSH, &newtermios) == 0);
}
bool cooked_stdin() noexcept {
return tcsetattr(STDIN_FILENO, TCSAFLUSH, &old_tios) == 0;
}
This is just ripped from a short program of mine... call raw_stdin() to try to put the terminal in raw-mode; you can read individual characters like this.
1 2 3 4
char c {};
if (sizeof c != read(STDIN_FILENO, &c, sizeof c))
perror("read(): ");
}
You need to make sure that the old_tios object gets initialized: