Need Help: 1-char input in C

Morris Keesan keesan at bbncca.ARPA
Fri Dec 23 09:07:50 AEST 1983


----------------------------
>   I want to have it so that the program can key an ASCII value for the key
>   from the VT100 terminal without having to have the user type RETURN.
>   
>   I tried using getchar() but inputs an entire line up to the RETURN, returns
>   the first letter, and shoves the rest in a buffer that is emptied the next
>   time getchar() is called.
>   
>   I just read that CP/M's version of C returns the value of the key as soon
>   as the key is typed, but UNIX C (which I am using) waits for a newline.
>   Yet, I know that many C utilities do accept a key as soon as I type it.
--------------------------------------------------------
    This is very simple, and it's nothing to do with C, but rather a UNIX(tm)
feature.  In normal modes, the UNIX terminal driver will do just this, hold
the input until a newline is input, and then make it available to the
program.  What you want to do is set CBREAK mode, which will make each
character available as it's typed.  Try setting in from the command line by
typing
	stty cbreak
to see if it behaves the way you want it to, and then put it into your program
using gtty() and stty(), or ioctl().  See ioctl(2) and tty(4).  You should
get the mode settings first, remember them, then set CBREAK, and restore the
original modes when you're through.  Note that CBREAK will effectively disable
any line editing (such as erase and line-kill).
-- 
					Morris M. Keesan
					{decvax,linus,wjh12}!bbncca!keesan
					keesan @ BBN-UNIX.ARPA



More information about the Comp.lang.c mailing list