home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.os.linux
- Path: sparky!uunet!gatech!taco!jlnance
- From: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
- Subject: Re: Polling keyboard in linux. What works best?
- Message-ID: <1992Aug31.124931.8528@ncsu.edu>
- Originator: jlnance@volt.ece.ncsu.edu
- Keywords: linux
- Lines: 56
- Sender: news@ncsu.edu (USENET News System)
- Reply-To: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
- Organization: North Carolina State University, Project Eos
- References: <17nf23INNfr8@matt.ksu.ksu.edu>
- Date: Mon, 31 Aug 1992 12:49:31 GMT
-
-
- 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);
- }
-