Pregunta

I started using ncurses by creating a test program so I could map what input I could expect from the function and implement a control using function pointers. However, after having the data I needed, the program having the before mentioned control structure was not responding as expected. The first issue I noticed was that when using the arrow keys, the program displays text after each press. The second problem I noticed was that the values being shown in my own debug printouts indicate that the values for getch are different in the second program than the first.

Both programs have the same init functions with the same args in the same order, and are on the same machine, so there should be no reason why the output and behaviour should be different.

Does anyone know why this might be happening?

EDIT: This system is running on Linux Mint 16 x86_64 architecture. I compile with gcc using eclipse. libncurses is the only library included in the projects.

This is the code for the test:

int main()
{
 int i = 0;
 initscr();
 raw();
 keypad( stdscr, TRUE );
 noecho();
 while( 1 ) // need to catch all available keys
 {
  i = getch();

  mvprintw( 0, 0, "%d    ", i );

 }

 getch();
 endwin();
 return 0;

}

The output from this, when pressing the up key is: "259"

The other program which has the same code split among different files gives this output: "got 27 OA" and adds "^[OA" every time the up key is pressed again as if its echoing when it was configured to not.

I created another test program, but my simplifications have yet to reproduce the behaviour.

I removed portions of the code that the inclusion of this function which is planned on being called when pressing enter is causing/uncovering the change. Right now, the function is not associated with input, but still seems to be affecting the program's interpretation of the input.

void select( int n, Point *p )
{
 if( 16 == p->Y )
  phase = SETUP;

 else if( 18 == p->Y )
  ;

 else
  phase = QUIT;

}
¿Fue útil?

Solución 2

After the testing I have done to track this down, the problem comes down to the name of the function that causes the behaviour. This is probably due to there being another function called select that becomes overridden within ncurses. This helps explain the odd behavioural issues, where the input is not raw and echoing is still enabled after being explicitly configured otherwise.

So this problem has a simple solution: Do not use the name "select" in a scope that contains ncurses.

Otros consejos

A quick experiment:

#include <stdio.h>
#include <curses.h>
int main(void) {
   printf("KEY_UP = %d\n", (int)KEY_UP);
}

indicates that 259 is the value of KEY_UP, the value that getch() normally returns when it reads an up-arrow (since there's no single ASCII or even Unicode code for that key).

At least in xterm, there are two distinct sets of code sequences for the arrow keys. The up arrow key normally sends the sequence ^[[A, where ^[ is the Escape key. But when I enable "Application Cursor Keys" in xterm's middle-click menu, the up arrow key instead sends ^[OA, which is consistent with the 27 OA you're seeing.

So if getch() sees the ^[[A sequence, it recognizes it as an up-arrow key and returns KEY_UP. But it doesn't recognize ^[OA, so it just returns it as a sequence of 3 distinct characters. (The sequence that getch() recognizes is controlled by the terminfo entry for your terminal type, depending on the value of $TERM.)

Find out how to disable "Application Cursor Keys" in your terminal emulator; that should fix the problem.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top