您好,我有这段代码,并且退格键无法正常工作

 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();


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

最佳答案

如果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取决于您使用的系统)。

关于c - 使用退格键的NCurses,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43946959/

10-11 21:56