Valores clave extraños impresos por ncurses

I am doing a little program in C with the ncurses library on Linux.

I decided to check the input I received with the getch () function, more specifically, the Retroceso clave.

La Retroceso ASCII decimal value is 127, Enlace: aquí I decided to print the numerical decimal value of the keys I pressed, for example:

a -> 97

A -> 65

] -> 93 ...

The latter are correct.

However, the following values are no correcto:

Backspace -> 7 (which is BELL)

Supr -> 74 (which is 'J')

Aquí está el código de prueba:

#include <curses.h>

int main(int argc, char **argv)
{
  char ch;
  int column,line;
  int s_column,s_line;
  initscr();
  clear();
  noecho();
  raw();
  keypad(stdscr,TRUE);
  printw("Type: \n> ");
  refresh();
  getyx(stdscr,s_line,s_column);
  while((ch=getch())!='\n')
    {
      printw("%d",ch);
      addch(ch);
      refresh();
    }
  endwin();
  return 0;
}

NOTA: cambio crudo() a cbreak() generates the same output

Output test: (note: I type: 'a','A',(Backspace),(Supr),'J')

Type:
> 97a65A7^G74J74J

I don't understand why this is happening, can somebody explain why the Backspace key outputs 7 instead of 127, and Supr outputs 74, which is the same sa 'J'?

preguntado el 31 de diciembre de 12 a las 15:12

How is 74 both 'j' and BELL -

I feel we need to see the code... And backspace may be mapped as "delete" (127) by the setup in termios. -

It's worth noting that you probably need to do keypad(stdscr, TRUE); to allow "Special" keys through. Not sure if this is what is the problem here. The keys outside "regular" ones are also outside of the range or "char", which may affect things. -

@MattiVirkkunen Yeah, I wrote that wrongly. It's corrected now, as well as a few other mistakes and the code and output of the program. -

Some terminal types (vt100, vt220?) had their BACKSPACE key mapped to DEL (=0x7f) Please take a look at the termcap entry for your terminal (xterm?, network?) -

2 Respuestas

For special function keys, getch() doesn't necessarily return the ASCII character, it returns one of the KEY_xxx códigos en <curses.h>. In the case of Backspace, this is:

#define KEY_BACKSPACE   0407        /* backspace key */

Desde que declaras ch as char más bien que int, el valor 0407 está siendo truncado a 07.

Cambie la declaración a:

int ch;

and then it will display 263 when you press Backspace. addch() todavía se mostrará ^G, though, because it doesn't use the KEY_xxx macros. You need to handle these characters in your code.

Respondido el 31 de diciembre de 12 a las 16:12

I believe the "special" keys are generating multi-character readings, which explains the ^ in the output.

Vea notación caret para más.

Respondido el 31 de diciembre de 12 a las 15:12

Backspace returns 7^G which is not its caret notation. - Asheeshr

You are right, for some reason Backspace is outputting 7 and ^G, but still, why is it outputting 7? according to your link it should be '^?' - Kosmos

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.