Curses and the arrow keys

Kim Christian Madsen kimcm at diku.dk
Mon Aug 27 08:54:07 AEST 1990


mlake at irscscm.UUCP (Marshall Lake) writes:


>I am writing a program which will be taking advantage of curses.  I am
>trying to utilize the arrow keys and am confused (actually all of
>curses confuses me).  I am doing a getch () and if I get an ESCAPE I do
>another getch ().  If I addch () for each character I get (the ESCAPE
>and the next character) then the cursor moves around properly on the
>screen but the x/y coordinates in the window structure do not follow
>suit.  Curses is recognizing the arrow keys simply as regular
>characters.  Am I naive in thinking that curses is smart enough to
>handle the arrow keys specially?  Should I be moving the cursor
>manually via the move function?

>I'm really not sure I know what I'm talking about when it comes to
>curses so any help is appreciated.

In the following I suppose that you have System V curses, I'm no real
expert on BSD curses.

The method you're using to catch the arrow-keys will work only if you
know exactly what the arrow-keys send, but hopefully they're described
properly in your terminfo database, so you could get the information
from there and parse it. But a better way to handle this situation is
using the following approach (I assume that we're using stdscr (the
default window).

	...
	initscr();		/* Enable curses */
	keypad(stdscr,TRUE);	/* Enable function-keys */
	noecho();		/* Do not echo typed input on screen */
	...
	switch (ch=getch()) {
	case KEY_DOWN:		/* Down arrow pressed */
		....
		break;
	case KEY_UP:		/* Up arrow pressed */
		...
		break;
	case KEY_LEFT:		/* Left arrow pressed */
		...
		break;
	case KEY_RIGHT:		/* Right arrow pressed */
		...
		break;
	default:
		if ((ch & A_CHARTEXT) != ch) 
			/* Function key was pressed */
		else	/* Normal character read */
		break;
	}

This answers your first question, I hope, as for your second question,
whether you have to move the cursor manually, after determining that
an arrow key has been pressed. The answer is YES! And the reason is
obvious, first only you know that it was an arrow key to curses it was
only a sequence of characters. Remember that almost every terminal of
a different brand has differing escape sequences associated with the
function keys (including arrow keys). Another vital reason for curses
to expect you to decide what to do when an arrow-key is pressed, is
that you might not always want it just to move the cursor on the
screen, you might want to bind the functionality of pulling a menu
down, when you hit the down arrow and then in the menu using it to go
to the next menu item, or you might want another functionality the
authors of curses never dreamed about, even in their nightmares, when
designing the system. So the bottom line is: Yes you can easily detect
when a function key has been pressed, but then you must decide what
you want to do with that information.

					Best Regards
					Kim Chr. Madsen

		



More information about the Comp.unix.questions mailing list