frank@altera.UUCP (Frank Heile) (01/28/88)
Problem: How to read a single character from the keyboard without blocking the task to wait for input, and WITHOUT REQUIRING THE USER TO TYPE A TERMINATING <RETURN>. My Current Solution: (using Manx 3.4b) ----------------------------------------------------------------- #include <stdio.h> struct FileHandle * Input(); main() { struct FileHandle * InHandle; char ch; InHandle = Input(); /* get the input file handle from DOS */ setbuf(stdin,NULL); /* don't let the stdio package buffer the input */ while (1) { /* this returns true if a character is available */ if (WaitForChar(InHandle,1L)) /* I'm afraid to use 0L */ { /* because of the Delay(0L) bug */ ch = getchar(); ...do something with this character... ...like change the mode...or like EXIT... } else { ...continue to do something else... } } } ----------------------------------------------------------------- (This is from memory...please excuse any errors) THE PROBLEM: Apparently WaitForChar() and the DOS Read() function will NOT recognise keyboard input until a <RETURN> has been typed by the user. The above code works just fine except that no characters are returned by getchar() until after a return is typed. DOS must be buffering up the input till a <RETURN> is typed. The getchar() eventually calls the DOS library Read() routine (I know because looked at the source since I bought the commercial package with library source included). So it appears that the DOS WaitForChar() and Read() are the culprits. I can't find anywhere in the RKM about how to make DOS not buffer the input - can anyone help? What I need is a simple technique to read a single character from the keyboard. This is for a very simple CLI type task that doesn't use intuition. I am sure that by using the intuition IDCMP port it could be done. But I would like a simpler method which doesn't require opening a window or interfacing to intuition.
louie@trantor.umd.edu (Louis A. Mamakos) (01/28/88)
Why don't you put the CLI console (or whatever, it sounds like you're trying to read from a CON: window) into RAW mode. Then the characters will be returned one at a time. Don't forget to return it to normal mode before you exit. Louis A. Mamakos WA3YMH Internet: louie@TRANTOR.UMD.EDU University of Maryland, Computer Science Center - Systems Programming
cherry@husc4.HARVARD.EDU (michael cherry) (01/29/88)
This is what I use. ---- #include <sgtty.h> #include <stdio.h> main() { struct sgttyb stty; struct sgttyb ostty; char c; /* put the console in RAW mode */ ioctl(0, TIOCGETP,&stty); ostty = stty; stty.sg_flags |= RAW; ioctl(0,TIOCSETP,&stty); while(1) { c = getchar(); /* read a character. Will get each character as they are typed */ /* do something with the character */ } /* return the console to its original state */ ioctl(0,TIOCSETP,&ostty); }