ncurses- KEY_ENTER is fail

前端 未结 4 1308
灰色年华
灰色年华 2020-12-20 13:56

I\'ve been trying to teach myself ncurses and I\'m loving it so far. However, I\'m trying to write a small little text editor like pico or nano. I\'ve got it set up fairly

相关标签:
4条回答
  • 2020-12-20 14:40

    Try 10 as ASCII value ... worked for me on ncurses.Also please update the actual code because this code that you put is wrong by syntax.

    0 讨论(0)
  • 2020-12-20 14:45

    From the PDCurses documentation:

    #define KEY_ENTER 0x157 /* enter or send (unreliable) */
    

    Try calling nonl() after raw().

    The nl and nonl routines control whether the underlying display device translates the return key into newline on input, and whether it translates newline into return and line-feed on output (in either case, the call addch('\n') does the equivalent of return and line feed on the virtual screen). Initially, these translations do occur. If you disable them using nonl, curses will be able to make better use of the line-feed capability, resulting in faster cursor motion. Also, curses will then be able to detect the return key.

    0 讨论(0)
  • 2020-12-20 14:59

    The likely problem is user confusion between the Enter key on the regular keyboard versus the Enter key on the numeric keypad. Those could both send a control/M (13), but not necessarily. The terminal description and KEY_ENTER refer to the numeric keypad.

    The ncurses manual page for getch explains the behavior in the NOTES:

    Some keys may be the same as commonly used control keys, e.g., KEY_ENTER versus control/M, KEY_BACKSPACE versus control/H. Some curses implementations may differ according to whether they treat these control keys specially (and ignore the terminfo), or use the terminfo definitions. Ncurses uses the terminfo definition. If it says that KEY_ENTER is control/M, getch will return KEY_ENTER when you press control/M.

    Generally, KEY_ENTER denotes the character(s) sent by the Enter key on the numeric keypad:

    • the terminal description lists the most useful keys,

    • the Enter key on the regular keyboard is already handled by the standard ASCII characters for carriage-return and line-feed,

    • depending on whether nl or nonl was called, pressing "Enter" on the regular keyboard may return either a carriage-return or line-feed, and finally

    • "Enter or send" is the standard description for this key.

    Line-feed, by the way, is a 10. But in C, it is usually shown as '\n' (and carriage return as '\r').

    0 讨论(0)
  • 2020-12-20 15:02

    I got the same KEY_ENTER problem recently, and I fixed it by replacing KEY_ENTER with 10 or \n, which is ASCII new line.

    #include <ncurses.h>
    int main() {
        initscr();  /* init ncurses */
        keypad(stdscr, TRUE);   /* get keyboard input */
        addstr("Press enter to exit.\n");
        while (10 != getch()) {}    /* 10 == enter */
        endwin();   /* end ncurses */
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题