ESC key in curses

ESC key in curses

Post by Ted Hoa » Thu, 18 Jul 1991 02:05:08



        Someone can tell me how to detect the 'ESC' key in curses.
        Thankyou,

        Ted Hoang
        LPA
        Houston, TX

 
 
 

ESC key in curses

Post by Mike Stefan » Sat, 20 Jul 1991 06:01:38



Quote:>Someone can tell me how to detect the 'ESC' key in curses.

All you could possibly want to know about fetching keys in curses:

In curses, you have two 'states' that a given window may be in: in application
keypad mode, and out of application keypad mode.  This is changed by using
the following function:

        keypad(win, state)

Where 'win' is the window in question, and 'state' is either TRUE or FALSE.

Whenever you create a window, either through initscr() which creates stdscr,
or the newwin()/subwin() functions, the default application keypad mode is off.
This means that subsequent calls to getch() will *not* resolve "application"
keys, such as cursor keys or function keys.  If you want this to be done, then
you need to set the state to TRUE.

Keep in mind that getch() returns an *integer*, not a type char.  With
keypad application mode on, getch() will of course return values that are
outside of the range of 0-255.

Many modern terminals, especially the ANSI and VT terminals, return the
ESC (ASCII 27) character as part of the sequence, typically at the
beginning of the sequence.  For example, the "up arrow" key on an ANSI terminal
is "ESC [ A" (27 91 65).  In order to distinguish an ESC by itself, rather
than part of a longer sequence which identifies an "application" key, the
getch() sets a timeout value of 2 seconds; if ESC is pressed, and there are
no remaining characters after 2 seconds has expired, getch() will assume that
the escape was entered alone, and will return a value of 27.  If more
characters did follow the escape, but the pattern does not match any of the
known application keys, then a value of 27 will be returned (and subsequent
calls to getch() will return the other characters).

Here is a brief example:

#include <stdio.h>
#include <curses.h>

main()
{
    int ch;

    initscr();                 /* initialize stdscr */
    cbreak();                  /* cbreak mode (character at a time) */
    noecho();                  /* turn character echoing off */
    keypad(stdscr, TRUE);      /* turn application keypad mode on */

    while ( (ch = getch()) != 27 )
        switch ( ch ) {
            case KEY_UP:    printw("UP\n");    refresh(); break;
            case KEY_DOWN:  printw("DOWN\n");  refresh(); break;
            case KEY_LEFT:  printw("LEFT\n");  refresh(); break;
            case KEY_RIGHT: printw("RIGHT\n"); refresh(); break;
        }

    endwin();                  /* restore terminal to original state */
        return 0;

Quote:}

--
Mike Stefanik, MGI Inc., Los Angeles -- Opinions stated are never realistic!
Creeping featurism is the albatross around every programmer's neck.

 
 
 

1. sco-list: how can I detect ESC-2 key sequence in curses program

Generally speaking, this should be bound to some key defined in the
terminfo database and defined within curses.  If you are working with a
terminal or emulator that doesn't have function keys, then you will need to
write a terminfo entry for it with the appropriate codes defined for the
function keys, then compile it with tic.  You will have to consider timing
as the curses implementation expects the characters from function keys to
come in within a specified delay time (which is why vi may do strange
things over slow telnet or modem links unless you change its timeout
value).

Bill
--

UUCP:               camco!bill  PO Box 820; 2835 82nd Avenue S.E. S-100
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676
URL: http://www.celestial.com/

A child can go only so far in life without potty training.  It is not
mere coincidence that six of the last seven presidents were potty
trained, not to mention nearly half of the nation's state legislators.
                -- Dave Barry

2. Howto port a windows win32 program to linux sdl

3. Telnet from AIX 1.2 -> ESC key sends two ESC's

4. STB Velocity 4400 not supported by redhat 6.0 ? dual monitor , dual cpu

5. <Alt>+<key> = <Esc><key> ?

6. LILO uninstall -- need help!

7. Getting ESC from CURSES getch()

8. RedHat 5.1 PPP/Kernel Problem

9. Konsole and <ins> <del> keys

10. Ins key in ncurses applications?

11. Taking ESC keystroke apart from ESC sequence (reliable way)

12. Suse 7.2 ESC-ESC for commandoline fillout?

13. Howto get TAB to do filename completion and not ESC ESC??