login
Header Space

 
 

Re: Polling keyboard in linux. What works best?

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
Date: Monday, August 31, 1992 - 8:49 am

In article <17nf23INNfr8@matt.ksu.ksu.edu>, kxb@matt.ksu.ksu.edu (Karl Buck) writes:
|> 
|> I want to check the keyboard for input (using gcc of course) without
|> actually reading (ie: getc etc..). The unix FAQ suggests looking at
|> the FIONREAD ioctl or using select() and the C FAQ states that this is
|> a purely OS dependant question.
|> 
|> What is best for Linux? Can this be non-blocking? A snippet of code
|> for this would be great since I have not had to do this kind of thing
|> before. Thanks.
|> 

I was given the following segement of code when I asked this question.  I have
been told that it works on a DECstation, but I have never tested it under 
Linux.

Hope it helps,

Jim Nance

#include        <sgtty.h>
#include        <fcntl.h>

struct sgttyb normal_mode, cbreak_mode;

set_trap()
{
static int first_time = 1;
if (first_time)
        {
        ioctl(0, TIOCGETP, &normal_mode);
        cbreak_mode = normal_mode;
        cbreak_mode.sg_flags |= CBREAK; /* make characters available as typed */
        cbreak_mode.sg_flags &= ~ECHO;  /* turn off echo */
        first_time = 0;
        }
ioctl(0, TIOCSETP, &cbreak_mode);
fcntl(0, F_SETFL, FNDELAY);
}

unset_trap()
{
fcntl(0, F_SETFL, 0);
ioctl(0, TIOCSETP, &normal_mode);
}

chk_buf()       /* read a character from the keyboard buffer */
                /* return '\0' if nothing available */
{
char c;
int status;
status = read(0, &c, 1);
if (status == -1) return(0);
else return(c & 0x7f);
}
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: Polling keyboard in linux. What works best?, JAMES LEWIS NANCE, (Mon Aug 31, 8:49 am)
speck-geostationary