Using backspace with ncurses

I have a simple ncurses program that reads characters one at a time using getch () and copies them to the buffer. The problem I am facing is when the backspace key is pressed. Here is the relevant code:

while((buffer[i] = c = getch()) != EOF) {
    ++i;
    if (c == '\n') {
        break;
    }
    else if (c == KEY_BACKSPACE || c == KEY_DC || c == 127) {
        i--;
        delch();
        buffer[i] = 0;
    }
    refresh();
}

But when you try to run this code, this is what appears on the screen after trying to remove characters from the string "this is a test":

this is a test^?^?^?

and contents buffer:

this is a test

With gdb, I know that an if statement is called that checks for the removal of / backspace, so what else should I do so that I can delete characters?

+5
source share
1 answer

It appears to ^?correspond to an echo on the screen when you enter the DEL character.

, delch(), , ( ) .

noecho() .

+4

All Articles