
On 24/01/10 20:49, Matthias Güdemann wrote:
Hi,
I wrote a program which should read the number of key presses in a given time. It forks a thread which calls getChar and increases a MVar each time getChar suceeds. After a delay (here 2s) the MVar is read, its value is printed and the program exits.
import Control.Concurrent
increaseEveryPush mVCount = do _ <- getChar inh <- takeMVar mVCount putMVar mVCount (inh + 1) increaseEveryPush mVCount
main = do x <- newMVar 0 forkIO (increaseEveryPush x) threadDelay 2000000 inh <- takeMVar x putStr $ "\n" ++ (show inh) ++ " times button pushed\n"
But this does not work, as getChar does not succeed if there is no EOF given (^D). It normally just exits with 0 as value in the MVar. Is there a way toread the keyboard without having to terminate with EOF?
In the past I've done this sort of thing by turning of buffering on stdin: hSetBuffering stdin NoBuffering /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus@therning.org http://therning.org/magnus identi.ca|twitter: magthe