1

I have this function that gets one character input in the terminal without the user having to press Enter. I can detect ALT + another key by checking for '\033' (escape), but how do I check if user pressed CTRL + another key?

int getch() {
    struct termios oldt, newt;
    int ch;
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    ch = getc(stdin);
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    return ch;
}
adsf
  • 59
  • 5
  • Control characters have ASCII codes between 0 and 31. Ctl-a is 1, ctl-b is 2, etc. – Barmar Dec 22 '22 at 22:33
  • Some combinations like Ctrl-C and Ctrl-Q doesn't work. How do I fix this? I understand why Ctrl-C work because that terminates the program. But I don't see why Ctrl-Q doesn't work? Ctrl-\ and Ctrl-s also causes problems. – adsf Dec 22 '22 at 22:39
  • You need to put the terminal in raw mode to disable the special meaning of those control characters. – Barmar Dec 22 '22 at 22:41
  • Is there a reason you're not using curses? – Blabbo the Verbose Dec 22 '22 at 23:16
  • Possible dupe of https://stackoverflow.com/questions/9750588/how-to-get-ctrl-shift-or-alt-with-getch-ncurses – ktqq99 Dec 23 '22 at 11:44

0 Answers0