0

I am currently writing an ncurses shell and in order to read input it is important to read it character by character and hence I am using the mvwgetch command. And incrementing a counter as it reads character by character. The problem is that whenever I press a an arrow key or a backspace their output is being printed. So, if for example I press backspace, ^? is being printed.

while ((command[i] = mvwgetch(promptwin, promptline, posx)) != '\n') {
    if (command[i] == 7) { // if entered character is a backspace
        i =-2;
        posx =- 2;
        mvwdelch(promptwin, promptline, posx);
        mvwdelch(promptwin, promptline, posx - 1);
        command[i] = '\0';
    } else {
        posx++;
        posyx[1] = posx;
        wmove(promptwin, promptline, posx);
    }
    i++;
}

It is required to read characters in order to keep track of where the cursor is found on the screen. In my code, there is my attempt at solving this problem but it still is displaying these characters. PS: working on linux.

griz
  • 119
  • 7
  • how are you initializing your nCurses shell? Are you using `noecho` in your program? Could you please share more code ? It would be a bit helpful for us to figure out. It would be nice if you use [predefined maros](https://docs.oracle.com/cd/E19455-01/806-0629/6j9vjcnt9/index.html) instead of hard coded values for key codes : – Rohan Kumar Apr 27 '17 at 14:16
  • What part should I upload? I am not using `noecho` because otherwise, it won't print on the shell. – griz Apr 27 '17 at 14:44
  • Actually i was interested in debugging your code myself. You can share the file from which you have posted this snippet. You can share code via pastebin if you don't want to spam your post. – Rohan Kumar Apr 27 '17 at 14:48
  • hmm. Adding `mvwscanw(promptwin, promptline, posx, "%[^\n]", command);` would solve the problem of backspace, but it would not be able to handle arrow keys correctly. – Rohan Kumar Apr 27 '17 at 17:05
  • and the problem with that is that I want to know the position of the cursor because the program updates a separate window every second and therefore the x coordinate is passed through a shared memory segment which then it is moved back to the position it left. – griz Apr 27 '17 at 18:45

2 Answers2

0

To start with, 7 is not backspace -- 7 is the bell. You want 8. You might also check for KEY_BACKSPACE.

You mention not using noecho(), but that's exactly what you have to do to suppress the output of special characters. Then you can explicitly addch() the ones you want to appear (the printable characters).

William McBrine
  • 2,166
  • 11
  • 7
0

SOLVED

Turns out the problem was that the code for backspace is 127. Therefore it was not being recognised. To handle backspaces it now executes the following code.

if(c == 127 || c == 8){                     //if character inserted is backspace or delete
                        if(posx != tcount) {
                            mvwprintw(promptwin, promptline, (posx + 1), " ");
                            mvwprintw(promptwin, promptline, posx, " ");
                            mvwprintw(promptwin, promptline, (posx - 1), " ");
                            wmove(promptwin, promptline, (posx - 2));
                            command[(chara - 1)] = '\0';
                            chara--;
                            posx--;
                            posyx[1] = posx;
                        } else {
                            mvwprintw(promptwin, promptline, (posx + 1), " ");
                            mvwprintw(promptwin, promptline, posx, " ");
                        }
                    } else {
                        command[chara] = c;
                        posx++;
                        posyx[1] = posx;
                        wmove(promptwin, promptline, posx);
                        chara++;
                    }
griz
  • 119
  • 7