簡體   English   中英

使用退格鍵的NCurses

[英]NCurses using backspace

您好,我有這段代碼,並且退格鍵無法正常工作

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

沒有出現退格字符,這是我想要的,但操作未完成

如果在getch調用之前打開echo ,則ncurses可能返回ASCII退格鍵,例如8(與\\b相同)。 源代碼中提到的是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取決於您使用的系統)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM