Processing arrow keys, function keys, etc.

Processing arrow keys, function keys, etc.

Post by Peter Chapi » Tue, 11 Feb 1997 04:00:00



This might be a stupid question, but here it goes.

I'm working on a program that needs to process arrow keys, function keys,
and other special keys -- if they are available. What is the best way of
determining if such keys are available on the user's terminal and, if so,
what sequence of characters I can expect when they are pressed?

I looked at the curses library, but that seems mostly interested in
terminal independent output. It appears to have little to say about input.
I also looked a little at the lower level stuff (terminfo, termcap), but
again the information I'm interested in doesn't seem to be discussed there
either.

Am I missing something here?

TIA

*****************************************************************************
Peter                                    http://twilight.vtc.vsc.edu/~pchapin

 
 
 

Processing arrow keys, function keys, etc.

Post by Andrew Giert » Wed, 12 Feb 1997 04:00:00


 Peter> This might be a stupid question, but here it goes.

 Peter> I'm working on a program that needs to process arrow keys,
 Peter> function keys, and other special keys -- if they are
 Peter> available. What is the best way of determining if such keys
 Peter> are available on the user's terminal and, if so, what sequence
 Peter> of characters I can expect when they are pressed?

You can get this info from termcap/terminfo.

 Peter> I looked at the curses library, but that seems mostly
 Peter> interested in terminal independent output. It appears to have
 Peter> little to say about input.  I also looked a little at the
 Peter> lower level stuff (terminfo, termcap), but again the
 Peter> information I'm interested in doesn't seem to be discussed
 Peter> there either.

The input sequences are just stored as string capabilities in the
terminal description (whether termcap or terminfo). You can read
the strings using the appropriate library functions (e.g. tgetstr).

Curses knows how to recognise these strings in keyboard input; if
you set keypad() true, then getch() will return values such as
KEY_UP or KEY_F(5) for the appropriate keys if they are used.
This is usually the easiest way.

HTH

--
Andrew.

 
 
 

1. Distinquishing escape key from arrow and function keys

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

2. How configure 'man' pages?

3. arrow key function in CDE?

4. Complex tape blocking question

5. How to get Function/Arrow Keys? Source wanted

6. app-starting problem in kde

7. VT220 termcap entry wanted, incl. arrow keys etc.

8. sed or awk

9. Function keys under X-Windows: Why do the generate the keys they do?

10. Function of Keys ALT-CTRL key combinations

11. Escape key timing issue and terminal function keys OSR5

12. Mapping keys other than the function keys

13. OpenSSL Key Encryption/ Session Key/ etc