Using arrow keys with curses.

der Mouse mouse at mcgill-vision.UUCP
Mon Jun 13 19:37:06 AEST 1988


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 could use something like 'hjkl' as my cursor moving keys, but I
> wanted to be able to write to the screen as the default.  If I used
> 'hjkl' I'd need a special "write" key to signal that I wanted to
> write on that spot.

This sounds like a primitive flavor of text editor.  You might consider
looking at how existing editors do this.  Vi, for example, does what
you mention in that paragraph: depend on being in one or another of a
set of "modes" to tell what a given key (h, for example) does.  Emacs
uses (usually fixed) control characters, like ^B for moving left one
character.  Other editors (I'm not familiar with any others) may have
other conventions.  Some editors doubtless go to the trouble to read
multi-character key sequences and do the appropriate thing; I believe
both vi and emacs can be set up to do this sort of thing.

You pays your money and you takes your choice, sort of.  You can opt
for one of the simple solutions: simple to implement, somewhat rougher
to use.  Or you can go for something more complex: more work for the
programmer but less pain for the user.  It's a design tradeoff; look at
what it'll be used for and such and take your pick.

					der Mouse

			uucp: mouse at mcgill-vision.uucp
			arpa: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list