Search code examples
cncurses

ncurses C home and end keys


I'm doing a simple menu with ncurses. It works for cygwin, but not in linux - home or end keys end the program immediately. I think handling of excape (27) has something to do with it because if I don't handle it in linux home and end have no effect. in cygwin - home, end and esc work fine (of course escape usually works which delay as it aways did in linux).

in my event loop I do this

  while (true) {
    clearScr = false;
    c = wgetch(menuWin);
    switch (c) {
      case KEY_HOME: {
        code = menu_driver(menu, REQ_FIRST_ITEM);
        break;
      }
      case KEY_END: {
        code = menu_driver(menu, REQ_LAST_ITEM);
        break;
      }
      case 27: {// Esc key - if I remove this home and end still doesn't work but they don't quit the program
        if (!isSubMenu) {
          // Handle cancel logic
          //are you sure?
        }
        return;
      }
...

I'm doing this when I create the menu

keypad(menuWin, TRUE);

my ncurses init/cleanup code:

void initialize_ncurses() {
  initscr();
  cbreak();
  noecho();
  keypad(stdscr, TRUE);
  intrflush(stdscr, FALSE); // Prevent flush on interrupt keys - ChatGPT advice
  start_color();
  init_pair(1, COLOR_WHITE, COLOR_BLUE);
  init_pair(2, COLOR_YELLOW, COLOR_BLUE); // Selected text color pair
  init_pair(3, COLOR_WHITE, COLOR_BLACK);
}

void cleanup_ncurses() {
  endwin();
}

I'm connecting to linux via putty. putty keyboard settings:

enter image description here


Solution

  • The terminal as set by the TERM environment variable must match the terminal you are using. Linux ships with many different putty configurations, but as @ThomasDickey says, there may be putty configurations that do not exist.

    putty               putty+fnkeys+sco    putty-m1     putty+screen
    putty-256color      putty+fnkeys+vt100  putty-m1b    putty-screen
    putty+fnkeys        putty+fnkeys+vt400  putty-m2     putty-vt100
    putty+fnkeys+esc    putty+fnkeys+xterm  putty-noapp
    putty+fnkeys+linux  putty+keypad        putty-sco
    

    I would be inclined to try TERM=putty+fnkeys+xterm (with putty set to Xterm R6 emulation). You can also try TERM=xterm or TERM=vt100 and see if those might work.