Reading escape sequences (arrow keys and escape alone)

David Wright dwright at wheaton.UUCP
Sun Aug 12 12:26:16 AEST 1990


System: Ultrix 2.0, vt100 terminals.

     I am currently writing a C program that takes as input an escape alone, 
 and escape sequences (such as the arrow keys generate, e.g. ^[[A).  When my
 program receives an escape I go into non-blocking mode to check and see if
 there are any other key-strokes availabile.  If there aren't then I assume
 that an escape by itself has been pressed.  If there are, I read them to
 find out which arrow key was pressed (assuming it was an arrow-key).

 Unfortunately, this doesn't work to well.  If an arrow-key is pressed the
 individual key-strokes sometimes arrive at a considerable interval.  When I
 check in non-blocking mode to see if there is a character after the initial
 escape, the next character (e.g. "[") sometimes has not arrived yet, so
 that it appears as though an escape only was pressed.

 I have tried to read continuously in non-blocking mode after an escape was
 pressed.  This guarantees that I will see the arrow-key, if one was
 pressed. However this makes for awkward parsing when I try to see whether
 an escape only was pressed.  If the user presses an escape, followed by a
 regular character, I have provide for 'ungetting' that character in some
 way. This is not too hard but seems to lack elegance. Also, it becomes very
 difficult if the escape is followed by something like another arrow-key.
 Then there are three characters available after the escape, and this is
 a mess.

 What I am wondering is if there is some elegant solution for waiting a set
 period of time after esape is pressed?  Then I could wait for a while, say
 1/2 second, and check if there are any characters after the escape.  I have
 tried a sleep(1), but this can result in a wait of up to 2 seconds.

 I know there is way of doing what I want to do on SYS V. (Turning off
 ICANON and using VMIN and VTIME).

 Please respond via E-mail, unless you think it's something everyone would
 benefit from.  I will sumarize.  Sections of code welcomed.

 Thanks in advance for your help,
 
 David Wright, dwright at wheaton.uucp



More information about the Comp.unix.wizards mailing list