0

I am writing a ncurses program in C++, which allows the user to press keybindings to execute tasks. However, because there are too many keybindings, I decided to add some modifier keys. One of the most used modifiers is the Shift key. However, I have to implement a way to detect whether Shift is pressed or not.

This is a sample program I've been trying:

#include <ncurses.h>

int main() {
    initscr();

    raw();
    noecho();

    char ch = getch();
    printw("%c", ch);
    refresh();

    getch();
    endwin();
}

When I press Ctrl-a, it shows ^A. Then, I tried Ctrl-Shift-a, but I still got ^A. I also tried a bunch of other keys, but it seemed like my program cannot distinguish between Ctrl-key and Ctrl-Shift-key events.

Is there a way to detect the Shift key? Thanks in advance!

Andy Zhang
  • 198
  • 4
  • 21
  • 1
    Your program gets bytes that the terminal sends (with or without ncurses). Terminals generally do not send different things for Ctrl-A and Ctrl-Shift-A. For some control keys such as arrows and F-keys, maybe, but not for letters. – n. m. could be an AI Dec 23 '22 at 11:59
  • @ktqq99 I tried the method. It said that the letter will be in caps if shift is pressed, which isn't the case here. – Andy Zhang Dec 23 '22 at 13:13
  • "It said that the letter will be in caps" I don't think there is any such claim in any of the answers. It is simply not true. You cannot detect the shift key with control characters (unless you run a special terminal). – n. m. could be an AI Dec 23 '22 at 14:49
  • Ok, turns out shift detection works for ESC-key combinations, but that's not the case for CTRL-characters. – Andy Zhang Dec 27 '22 at 01:02

0 Answers0