home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!bcm!rice!nb.rockwell.com!wade
- From: wade@nb.rockwell.com (Wade Guthrie)
- Newsgroups: comp.unix.programmer
- Subject: Re: checking stdin for keypress
- Keywords: keypress stdin
- Message-ID: <1992Sep11.162534.2728@nb.rockwell.com>
- Date: 11 Sep 1992 16:25:34 GMT
- References: <BuDH4F.LFv@news.cso.uiuc.edu>
- Sender: wade@nb.rockwell.com (Wade Guthrie)
- Organization: Rockwell International
- Lines: 58
-
-
- In article <BuDH4F.LFv@news.cso.uiuc.edu>, jrpg8255@uxa.cso.uiuc.edu (Jeremy Payne ) writes:
- >
- > How does one check for
- > keystrokes in the [UNIX tty] buffer?
-
- This is done by talking to the tty driver (one uses the 'ioctl' system
- call -- short for I/O control, but usually pronounced 'eye-octal' -- to do this
- under UNIX). TFM to R is 'man 4 termio', but I'll try to summarize here.
-
- ioctl is called with three arguments: the file descriptor, the request (both
- are integers) and the argument to the request (check out man 2 ioctl).
-
- If I remember right, you need to call 'ioctl(fd, TCGETS, &termios)' to get the
- value of the 'termios' structure. You then replace termios->c_cc[VMIN] (this
- indicates the minimum characters to read on that file descriptor) with 0
- and termios->c_cc[VTIME] (this indicates the time before timeout in 0.10 second
- intervals) with 0. Next, you call 'ioctl(fd, TCSETS, &termios)' to reset
- the values for the file descriptor.
-
- Once you've done this, you can do a read and check for the number of characters
- returned. In conjunction with buffered I/O, I'm not sure how well this works;
- you may have to use 'read' rather than 'getchar'.
-
- It's been awhile, so you may want to check out the man pages before you try
- this at home.
-
- > (how are they buffered?)
-
- Well, how and whether keystrokes get buffered is a matter for the tty driver
- which is accessed by your program. Keystrokes go into a queue in the tty driver
- called the raw queue. The raw queue typically buffers keystrokes, including
- things like <delete> (others are interpereted, like <ctrl-c> until a newline
- is entered. When this happens, the raw queue is dumped into another queue
- called the cannonical queue; it is during this dump that the <delete> and other
- special characters are interpereted. All of this happens when the tty driver
- is in 'cooked' mode.
-
- Another couple of modes exist. The simplest is 'raw' mode (you could taste that
- one comming, huh?) wherein ALL characters that go into the raw queue are
- immediately flushed into the cannonical queue -- no interpretation involved.
- A mode in-between 'raw' and 'cooked' exists; this is called 'cbreak' mode. This
- mode is just like 'raw' mode except a <cntrl-c> still is interpreted as a <break>
- (hence the name of the mode).
-
- > On a similar note, could
- > someone mention if and how scan codes etc. are generated for things like
- > function keys etc.?
- > A quick RTFM is fine :-) I've never used curses BTW if
- > that turns out to be the answer ...
-
- 'curses' is a fine answer to the function key stuff, but may be an overkill.
- It may be the only portable way to solve that problem, however.
-
- Hope this helps
-
- Wade
- wade@nb.rockwell.com
-