I need to detect a keystroke, without the user pressing enter. What's the most elegant way?
I.e. If the user hits the letter Q, without pressing enter, the program does something.
In unix/posix, the standard way of doing this is to put the input into non-canonical mode with tcsetattr:
#include <termios.h>
#include <unistd.h>
:
struct termios attr;
tcgetattr(0, &attr);
attr.c_lflag &= ~ICANON;
tcsetattr(0, TCSANOW, &attr);
See the termios(3) man page for more details (and probably more information than you wanted to know).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With