<div dir="ltr">It's probably best to use dynamic-wind, turning the settings on in the before-thunk and off in the after-thunk, so you don't wind up with the terminal set wrongly when the process terminates, like this:<div><br></div><div>(dynamic-wind (lambda () (setup-port port)) (let loop ...) (lambda () (restore-port port))))</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Sep 4, 2019 at 12:05 PM Marc Feeley <<a href="mailto:feeley@iro.umontreal.ca">feeley@iro.umontreal.ca</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Marc<br>
<br>
<br>
<br>
> On Sep 4, 2019, at 11:34 AM, John Cowan <<a href="mailto:cowan@ccil.org" target="_blank">cowan@ccil.org</a>> wrote:<br>
> <br>
> The buffering setting has to do with buffering within the Scheme process, whereas the tty setting has to do with buffering within the operating system.  In order to get single-character reads from the terminal, you have to turn off both.<br>
<br>
Indeed.<br>
<br>
The tty settings can be changed by calling tty-mode-set! as shown below.<br>
<br>
Marc<br>
<br>
<br>
;;; File: console-read-one-char-at-a-time.scm<br>
<br>
;; run like this:<br>
;;<br>
;;   gsi console-read-one-char-at-a-time.scm<br>
<br>
(define (setup port)<br>
  (if (tty? port) ;; change the tty settings to read raw characters 1 at a time<br>
      (tty-mode-set! port<br>
                     #f ;; input-allow-special<br>
                     #f ;; input-echo<br>
                     #t ;; input-raw<br>
                     #f ;; output-raw<br>
                     0))) ;; speed<br>
<br>
(define port (current-input-port))<br>
<br>
(setup port)<br>
<br>
(let loop ()<br>
  (let ((c (read-char port)))<br>
    (pp (list 'typed c))<br>
    (loop)))<br>
<br>
<br>
</blockquote></div>