getting input from keyboard

Michael Stefanik mike at bria.AIX
Tue Jan 1 05:22:16 AEST 1991


In article <DAVIDD.90Dec30195511 at wolf.cs.washington.edu>, davidd at wolf.cs.washington.edu (David Doll) writes:
> Hello,
> 	Inside of some "C" code, I want to be able to get a single char(or 
> number) from the user without them having to type a carriage return.

I'm assuming that you have the termio stuff, but here is a little ditty
to get a single character from the keyboard:

#include <stdio.h>
#include <termio.h>

int getch()
{
struct termio   old, new;
char            c;
int             ret;

        ioctl(0,TCGETA,&old);
        memcpy(&new,&old,sizeof(struct termio));

        new.c_lflag &= ~ICANON;
        new.c_lflag &= ~ECHO;
        new.c_cc[VMIN] = 1;
        new.c_cc[VTIME] = 0;

        ioctl(0,TCSETA,&new);

        if ( read(0,&c,1) != 1 )
                ret = EOF;
        else
                ret = (int)c;

        ioctl(0,TCSETA,&old);
        return(ret);
}
-----------------------------------------------------------------------------
Michael Stefanik, Systems Engineer (JOAT), Briareus Corporation
UUCP: ...!uunet!bria!mike
"If it was hard to code, it should be harder to use!"



More information about the Comp.lang.c mailing list