I'm writing a test application for a console text input library. The idea of that part of the library is to just treat any input as a regular text input, even Ctrl+[letter] combinations. And for the most part, it works rather well. But for some reason, every time I try out Ctrl+C, regular input just stops working - I can continue reading input character by character via getchar and read, but with regular stdin, I seem to be only able to press Ctrl+C again, nothing happens when I just try to type like normal. I succeed at using std::cin by just hitting the return key though.
I tested it on Ubuntu, both as WSL in the Windows Terminal and on a full Ubuntu VM.
Here's the relevant part of my code:
termios tOld, tNew;
tcgetattr(STDIN_FILENO, &tOld);
tNew = tOld;
tNew.c_lflag &= ~(ICANON | ISIG | IEXTEN | IXON | ECHO);
tNew.c_iflag &= ~(IXON | IXOFF);
tcsetattr(STDIN_FILENO, TCSANOW, &tNew);
// further processing (reading via read and getchar and 'parsing' the input)
tcsetattr(STDIN_FILENO, TCSANOW, &tOld);
I wonder what I'm doing wrong.
The other possible cause for this error is this code that I use for processing ESC presses:
std::function<bool(unsigned)> fnWaitForNextKey =
[](unsigned iMillis) -> bool
{
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = iMillis * 1000;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
return select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
};
refer to The GNU C Library, 17.4.10 Noncanonical Input
In the terminal’s c_lflag and c_iflag, probably can try to use noncanonical input, and turns off most processing to give an unmodified channel to the terminal.
tNew.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
tNew.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
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