Using arrow keys with curses.

Greg Franks greg at xios.XIOS.UUCP
Thu Jun 16 22:38:59 AEST 1988


In article <1162 at mcgill-vision.UUCP> mouse at mcgill-vision.UUCP (der Mouse) writes:
>In article <485 at cieunix.rpi.edu>, curt at cieunix.rpi.edu (Curt Signorino) writes:
>> I've also been trying to access the arrow keys with curses, but
>> haven't quite got the hang of it.  What I'd had in mind was doing a
>> getchar() and then switching on the appropriate value of the arrow
>> key to the cursor moving routines.
>
>Arrow keys generally send multi-character sequences; doing just one
>getchar() will get just one character.  You'd need to look up the
>sequences the arrow keys send somewhere (termcap has capabilities for
>this: kl, kr, kd, ku).  Then if they are more than one character long,
>you will need to call getchar() multiple times and parse the results
>somehow.  It's not simple.
>

I don't know whether the original author stated which version of UNIX he
was on....

Sys V curses handles the arrow (and other function keys) automagically. 
Here are some code segments...

		initscr();		/* read terminfo database 	*/
		cbreak();		/* Cbreak mode.			*/
		noecho();		/* Don't echo chars		*/
		keypad( stdscr, 1 );	/* But do allow keypad ops	*/
		typeahead( 0 );		/* Allow typeahead.		*/

...
		int c;			/* NOT char!!!! */

		c = wgetch( stdscr );

		switch( c ) {

		case KEY_RIGHT: case 'L': case 'l':
		case KEY_DOWN:	case 'J': case 'j':
			cursor = curr_menu[cursor].next;
			break;

		case KEY_LEFT:	case 'H': case 'h':
		case KEY_UP:	case 'K': case 'k':
			cursor = curr_menu[cursor].prev;
			break;


**IMPORTANT** Don't use getc() or friends.  Use the curses equivalent. 
I don't know if this stuff is in Berkeley.

Good luck!
-- 
Greg Franks                   XIOS Systems Corporation, 1600 Carling Avenue,
utzoo!dciem!nrcaer!xios!greg  Ottawa, Ontario, Canada, K1Z 8R8. (613)725-5411.
   ACME Electric: When you can't find your shorts, call us!



More information about the Comp.lang.c mailing list