From: Steve Haflich

Subject: Re: Unbuffered keyboard reading

Date: 2000-3-28 14:10

   From: David Kaasen <nvg.ntnu.no at kaasen>
   
   How can I read a character from the keyboard
   (in xterm e.g.) without having to wait for
   newline (Enter)? Like the getch() function in
   many C libraries. I need this to write text
   mode games or editors, and I don't want to use
   ncurses, for instance. I am using ACL on both
   PC and Unix.
   
This question really is not specific to ACL, in the sense that the
question and much of the answer would remain the same if you were
programming in C or FORTRAN or even Java.  Input line buffering and
editing is not performed by your program; it is performed by the
operating system environment and the tool that is providing the shell
environment.

On Unix you can easily turn off input editing with either the stty(1)
command or the gtty(3)/stty(3) system routines.  If you are running
ACL in a plain xterm window, try playing the following quick hack:

 (unwind-protect
     (progn (excl:shell "stty raw")
            (loop repeat 20 do (print (read-char))))
    (excl:shell "stty cooked"))

If you want to be fancy, you could replace the stty invocation with a
ff call, but there is little advantage.  Either way, the stty details
may depend on the specific flavor of Unix.  On some you might also try
"stty cbreak" as more appropriate.  Your application might also want
to turn off echo.  See "man stty".

I don't know if there is an equivalent shell mode on Windows.

The whole problem with this kind of architecture, of course, is that
debugging is almost impossible during development is you are using the
same tty device for the debugging and application input.  Again, this
problem is not language specific.  I had to do _significant_ amounts
of debugging on a command-line application that needs to do
char-by-char input, I would use the Emacs-Lisp interface (which works
portably on both Windows and Unix) and customize a version of
fi:open-lisp-listener with a command table that does char-by-char
input for all the necessary chars.  (Explaining how to do this is not
trivial.)  But if the application is non trivial, then probably you
would want anyway to port it to a more ambitious user interface with a
development environment.