NOVICE question: How to know when a key is pressed

Rich Dykstra richd at hplvec.LVLD.HP.COM
Sun Apr 14 02:50:03 AEST 1991


On hp systems, two things must be done - change the terminal driver
configuration, and modify the terminal straps so that escape sequences
are  passed to the host instead of being acted upon locally by the
terminal.  Here is code I created several years ago, I think it is
still relevant: (refer to termio(7))




static  struct  termio  tcorg_block;
static  struct  termio  tcact_block;
static  struct  f_rec_t tcara[TCARA];
        struct  key_rec{    int key; char   chr};
tcinit()
{
    int result;

    printf("%c&s1A", 27);                       /* transmit esc sequences*/
    result = ioctl(0, TCGETA, &tcorg_block);    /* driver data (origial) */
    result = ioctl(0, TCGETA, &tcact_block);    /* driver dat (to mess with)*/
    tcact_block.c_lflag &= ~ICANON & ~ECHO;     /* no echo or pre process*/
    tcact_block.c_cc[VEOF] = 1;                 /* read 1 char at a time*/
    result = ioctl(0, TCSETA, &tcact_block);    /* set the driver       */
    tcmove(0,0,'S');                            /* clear the screen */
}



As an alternative, you may want to consider using the curses package. It
does this as well as offering other slick interfacing features, such as
terminal type independence.



More information about the Comp.lang.c mailing list