home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sun4nl!hgatenl!tedux!clifton!baron
- From: baron@clifton.hobby.nl (Wim `Blue Baron' van Dorst)
- Newsgroups: comp.os.linux
- Distribution: world
- Subject: Re: Polling keyboard in linux. What works best?
- References: <1992Aug31.124931.8528@ncsu.edu>
- Message-ID: <9209023072@clifton.hobby.nl>
- Organization: Minix Hobbynet Host
- Date: Wed, 2 Sep 1992 22:12:55 MDT
- Lines: 70
-
- > 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.
-
- jlnance@eos.ncsu.edu (JAMES LEWIS NANCE) writes:
- > I was given the following segement of code when I asked this question.
- > [ stuff deleted -b-b- ]
- > #include <sgtty.h>
- > #include <fcntl.h>
- > struct sgttyb normal_mode, cbreak_mode;
- > [ stuff deleted -b-b- ]
- > 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 */
-
- Sorry, but that won't work. I currently am working on a little game, and
- of course you have to test for key-pressing. I had code like the above working
- properly in Minix, but for Linux the implementation is purely Posix, which
- the above is not.
-
- I very easily changed my code to Termios (i.e., the Posix and thus Linux
- way of doing such things) for the what I wanted, using indeed the non-blocked
- read(). A very simple example is this:
-
- #include <sys/types.h>
- #include <termios.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdio.h>
-
- main()
- {
- int ch = 'a', counter;
- struct termios *tp;
-
- tcgetattr(0, tp);
- tp->c_lflag &= ~(ECHO | ICANON);
- tcsetattr(0, TCSANOW, tp);
- fcntl(0, F_SETFL, O_NONBLOCK);
-
- while (ch != 'q') {
- for (counter=0;counter<100000;counter++) /* */ ;
- if (read(0, &ch, 1) == -1)
- printf("Non-blocked ch=%c\n", (char) ch);
- else
- printf("Typed ch=%c\n", (char) ch);
- }
-
- tp->c_lflag |= (ECHO | ICANON);
- tcsetattr(0, TCSANOW, tp);
- fcntl(0, F_SETFL, O_ACCMODE);
- }
-
- Of course this code actually _reads_ the character: It is not a test
- whether a character is available. Using code like this, however, such an
- implementation could easily be made using a buffer for actually read
- characters, and passing them on only when wanted.
-
- Met vriendelijke groeten, Wim `Blue Baron' van Dorst
- ---------------------------------------------------------------------
- Blue Baron = Wim van Dorst, Voice (+31) 074-443937, (+31) 02152-42319
- (-: baron@clifton.hobby.nl tgcpwd@urc.tue.nl :-)
- ---------------------------------------------------------------------
-