NCurses使用退格

时间:2017-05-12 21:52:15

标签: c ncurses curses getch

你好我有这个代码,退格不能正常工作

 while((ch=getch())!='\n') {
    ++counter;
    noecho();
    if (ch == KEY_BACKSPACE || ch == KEY_DC || ch == 127) {

        counter--;
        delch();
        delch();
        input[counter] = '\0';
    } else
    {
        addch(ch);
    }
    refresh();

退格符的字符没有出现,这是我想要的但是动作没有完成

1 个答案:

答案 0 :(得分:1)

如果echogetch调用之前上,则ncurses可能会返回ASCII退格键,例如8(与\b相同)。这在source-code中作为Solaris兼容性的一个特性提到:

    /*
     * If echo() is in effect, display the printable version of the
     * key on the screen.  Carriage return and backspace are treated
     * specially by Solaris curses:
     *
     * If carriage return is defined as a function key in the
     * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
     * if nonl() is set) or KEY_ENTER depending on the echo() mode.
     * We echo before translating carriage return based on nonl(),
     * since the visual result simply moves the cursor to column 0.
     *
     * Backspace is a different matter.  Solaris curses does not
     * translate it to KEY_BACKSPACE if kbs=^H.  This does not depend
     * on the stty modes, but appears to be a hardcoded special case.
     * This is a difference from ncurses, which uses the terminfo entry.
     * However, we provide the same visual result as Solaris, moving the
     * cursor to the left.
     */
    if (sp->_echo && !(win->_flags & _ISPAD)) {
    chtype backup = (chtype) ((ch == KEY_BACKSPACE) ? '\b' : ch);
    if (backup < KEY_MIN)
        wechochar(win, backup);
}

另一种可能性是终端描述(terminfo)可能与实际终端设置(stty)不一致。在这种情况下,ncurses将返回密钥发送的任何内容(无论是ASCII DEL / 127还是BS / 8取决于您使用的系统)。