Keyboard Input

Rahul Dhesi dhesi at bsu-cs.bsu.edu
Sat Jul 22 10:26:41 AEST 1989


In article <539 at winnie.fit.edu> acs60222 at zach.UUCP ( ENRIQUEZ) writes:
>Hmmmm, it seems I have been misunderstood...

Oops!  Sorry about that.  I really did misunderstand.

>My question was is there a C [equivalent] to the READ (kbd,char) (not
>READLN, my mistake) [available] in pascal....
...
>All I want to do is get single-character input without having to
>hit return.

"How do I do single-character input without having to hit return" is a
Very Commonly Asked Question.

Probably the closest C analog to the Pascal construct 
"read (file, char-variable)" is something like this:

     c = getc (stdin);   /* can also use c = getchar() */

However, neither the Pascal nor the C construct guarantee "raw" input,
i.e., reading a character from the device without waiting for an entire
line to be typed.  To do raw input you must do something special to
tell the device driver not to wait for a complete line.

Under VAX/VMS you can use an $QIO system call to do raw input.

Under MS-DOS you must use an MS-DOS-specific "ioctl" system call to put
the device driver into raw mode.  (Or you can do an MS-DOS-specific raw
console input system call, for which there is no corresponding standard
library function in C.)

Under the **IX family you can either do an ioctl() (a different one) or
do something like

     system ("stty raw");  /* more portable, slower, than ioctl() */

to put the tty driver into raw mode.

Your best bet, therefore, is to consult both your operating system and
your compiler manuals.

Once you have told the device driver to do raw input, you may still
need to set your stdin stream to unbuffered with a setbuf() function
call.  Alternatively, you could use a low-level system call for the
actual read, which again is highly system-dependent and not part of the
C language.
-- 
Rahul Dhesi <dhesi at bsu-cs.bsu.edu>
UUCP:    ...!{iuvax,pur-ee}!bsu-cs!dhesi



More information about the Comp.lang.c mailing list