[comp.unix.programmer] Distinquishing escape key from arrow and function keys

bin@primate.wisc.edu (Brain in Neutral) (04/11/91)

I have a program where the escape key is significant, but I also
want to be able to use the arrow and functions keys (which typically also
begin with escape).  I would be interested in how any of you solve this
problem.

My solution (if you can call it that): when an escape character is seen,
keep reading until
	(i) characters read entirely match some special key.  return some
		value (I use values >= 0200) to indicate such.
	(ii) characters read fail to match any special key character sequence.
		return escape to caller and save any other characters read
		to a pushback queue.
	(iii) timeout after brief wait and neither (i) nor (ii) obtain.
		return escape to caller and save any other characters read
		to a pushback queue.

On a BSD system, select() does the timeout nicely.  I'm not so familiar
with System V.  Do you use the MIN and TIME special control character
values in the termio.c_cc[] array?  What about under POSIX?

-- 
Paul DuBois                               "The 'C' shell usually doesn't
dubois@primate.wisc.edu                   have job control." -- LAN TIMES

john@jwt.UUCP (John Temples) (04/13/91)

In article <4193@uakari.primate.wisc.edu> bin@primate.wisc.edu (Brain in Neutral) writes:
>On a BSD system, select() does the timeout nicely.  I'm not so familiar
>with System V.  Do you use the MIN and TIME special control character
>values in the termio.c_cc[] array?

System V curses works the way you describe, i.e., it can distinguish
between ESC and ESC[A, and return the application a single token for
either input.  What I don't know is how curses implements this.  On my
System V box, you cannot set MIN=1 and TIME=1 to get a .1 second
timeout on a single character read, nor should you be able to,
according to termio(7).  We had a discussion about this very topic
right here a few months back, and I don't think anyone knew how to do
it on System V, even though it obviously can be done.
-- 
John W. Temples -- john@jwt.UUCP (uunet!jwt!john)

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR/AA) (04/14/91)

As quoted from <4193@uakari.primate.wisc.edu> by bin@primate.wisc.edu (Brain in Neutral):
+---------------
| On a BSD system, select() does the timeout nicely.  I'm not so familiar
| with System V.  Do you use the MIN and TIME special control character
| values in the termio.c_cc[] array?  What about under POSIX?
+---------------

SVR3 curses will do this for you --- when you call keypad() on a window, it
will return special keys as #define'd values starting at 0400.  It has a
timeout of about a second (this may be tuneable via halfdelay()).

Some programs I use make use of VMIN and VTIME.  My own programs don't do
timeouts, but could fairly easily do them because they use select() anyway.
(On systems that support it, at least.)  My main technique for avoiding ESC
anomalies is to not use bare ESC for anything and to try to map things on to
keys without an ESC prefix... which is difficult on a DEC-compatible terminal.

++Brandon
-- 
Me: Brandon S. Allbery			  Ham: KB8JRR/AA on 2m, 220, 440, 1200
Internet: allbery@NCoast.ORG		(QRT on HF until local problems fixed)
America OnLine: KB8JRR // Delphi: ALLBERY   AMPR: kb8jrr.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery          KB8JRR @ WA8BXN.OH