Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!iuvax!bsu-cs!dhesi From: dhesi@bsu-cs.bsu.edu (Rahul Dhesi) Newsgroups: comp.lang.c Subject: Re: Keyboard Input Keywords: Keyboard , Input , Trapping Message-ID: <8366@bsu-cs.bsu.edu> Date: 22 Jul 89 00:26:41 GMT References: <20283@adm.BRL.MIL> <10557@smoke.BRL.MIL> <538@winnie.fit.edu> <8340@bsu-cs.bsu.edu> <539@winnie.fit.edu> Reply-To: dhesi@bsu-cs.bsu.edu (Rahul Dhesi) Organization: CS Dept, Ball St U, Muncie, Indiana Lines: 49 In article <539@winnie.fit.edu> acs60222@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 UUCP: ...!{iuvax,pur-ee}!bsu-cs!dhesi