I've tried this:
main = do
hSetBuffering stdin NoBuffering
c <- getChar
but it waits until the enter is pressed, which is not what I want. I want to read the character immediately after user presses it.
I am using ghc v6.12.1 on Windows 7.
EDIT: workaround for me was moving from GHC to WinHugs, which supports this correctly.
The Haskeline package worked for me.
If you need it for individual characters, then just change the sample slightly.
getInputLine
becomesgetInputChar
"quit"
becomes'q'
++ input
becomes++ [input]
Hmm.. Actually I can't see this feature to be a bug. When you read
stdin
that means that you want to work with a "file" and when you turn of buffering you are saying that there is no need for read buffer. But that doesn't mean that application which is emulating that "file" should not use write buffer. For linux if your terminal is in "icanon" mode it doesn't send any input until some special event will occur (like Enter pressed or Ctrl+D). Probably console in Windows have some similar modes.Might be a bug:
http://hackage.haskell.org/trac/ghc/ticket/2189
Yes, it's a bug. Here's a workaround to save folks clicking and scrolling:
So you can replace calls to
getChar
with calls togetHiddenChar
.Note this is a workaround just for ghc/ghci on Windows. For example, winhugs doesn't have the bug and this code doesn't work in winhugs.