Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

ncurses- KEY_ENTER is fail

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 well so far. I created a function to map the keys. No matter what I do I can not get a response from KEY_ENTER. Whenever I press it it just goes to the beginning of the currently line that I'm on. I've tried using raw(); and using 13 instead of KEY_ENTER no luck. All the other keys respond as expected. I would appreciate any advice. I've been staring at this trying to make it work forever. Thanks!

#include <stdlib.h>
#include <ncurses.h>

// gcc keymaps.c -lncurses -o keymaps
int main(){
    int ch;

    initscr();
    cbreak();
    noecho();

    keypad(stdscr,TRUE);

    while (ch = getch()) {
      switch(ch){
         case KEY_UP:
              addstr("Up
");
              break;
          case KEY_LEFT:
              addstr("Left
");
              break;
          case KEY_RIGHT:
              addstr("Right
");
              break;
          case KEY_BACKSPACE:
              addstr("Backspace
");
              break;
          case KEY_ENTER:
              addstr("You pressed Enter
");
          default:
            printw ("%u
", ch);
            break;
      }
    }
}
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ' ' (and carriage return as ' ').


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...