As was mentioned in that thread (now archived), there's no portable way to do it that will work for windows and linux/unix. But it can be done for any recent linux. This might work on mac's too, depending on the availability of cfmakeraw
The underlying problem is that C++ doesn't know anything about the underlying OS. If we want to change the way the OS feeds us input, we have to tell it. Thus there's no portable way to do this.
file rawterm.h:
1 2 3 4 5 6 7 8 9
#include <termios.h>
class rawTerm
{
termios initState; // the intial state of the terminal
public:
rawTerm();
~rawTerm();
};
file rawterm.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <fcntl.h>
#include <termios.h>
#include "rawterm.h"
rawTerm:: rawTerm() {
tcgetattr(0, &initState); // get term attrributes on fd 0 (cin/stdin)
termios workios = initState; // make a copy to work with;
cfmakeraw(&workios); // set flags to put term in 'raw' mode
tcsetattr(0, TCSANOW, &workios); // tell the OS to put change the term attributes
}
rawTerm::~rawTerm() {
// put the term back into 'cooked mode
tcsetattr(0, TCSANOW, &initState);
}
I think you can do a getch with just a couple of lines of assembly, and its gonna be non portable anyway, may as well make it that way without all the hoops.