简体   繁体   中英

How to stop backspace from appearing in nCurses using C?

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.

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).

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++;
                    }

This is just additional information as I cant put up comment yet. I have encountered similar issue I print the character myself discovered 8 backspace for windows terminal. In ncurses is 7 or ^G when using noecho or ^? when using echo.

after char ch = getch();user can catch backspace as 7 and do further process themselves under noecho mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM