如何防止输入被冲入输出?

How to prevent inputs being flushed into output?

我在 Ubuntu。当我在终端上 运行 ghci 并执行此操作时:

Prelude Control.Monad System.IO> forever $ getChar >>= print

结果是这样的:

a'a'
b'b'
C'C'
%'%'
\'\'
1'1'
''\''
"'"'
^X'\CAN'
^?'\DEL'
^CInterrupted.

也就是说,我在键盘上输入的字符被刷新到输出中。我怎样才能防止这种情况发生并且只有 print 作为作者?

为防止输入被冲入输出(或"echoed"),使用hSetEcho stdin False.

Prelude> import System.IO
Prelude System.IO> import Control.Monad
Prelude System.IO Control.Monad> hSetEcho stdin False
Prelude System.IO Control.Monad> forever $ getChar >>= print
'a'
'\n'
'b'
'c'

这可以用来做类似 read in a password 的事情。