[Haskell-beginners] getChar and keyboard reading
Magnus Therning
magnus at therning.org
Sun Jan 24 16:00:49 EST 2010
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
Url : http://www.haskell.org/pipermail/beginners/attachments/20100124/2ea9b110/signature.bin
More information about the Beginners
mailing list