Search code examples
haskellglutfreeglutyampa

Getting key input with Yampa and yampa-glut


The entire topic of AFRP sounds very exciting, and I am trying to start my own project in it. I found Yampa and yampa-glut, which seems to be a good binding to GLUT (As it seems, GLUT is the only graphics lib that runs on both, Windows and Linux).

The yampa-glut source comes with a simple example, which I have copied to study it. Then I wanted to extend it, to add a simple test for key events. The full source of it is here, there's nothing much new except for line 23 and the keys function:

keys = proc ev -> do
    rec
        e <- keyAction -< ev
        let s = event Nothing (Just . show) e
        res <- identity -< s
    returnA -< res

The expected result is a simple output on the shell whenever I press or release a key. But it doesn't happen, there is no output.

If I change the line before the last to this:

res <- delay 0.2 Nothing -< s

Then I get some key input, but not all of them, many are skipped.

I couldn't find some explanation of this, so I hope someone can explain to me where I went wrong and what I can do to get keyboard and mouse button input.


Solution

  • After quite a while now, I have found I have been thinking entirely the wrong way. Here would be an extension of the above for testing for pressing of the "ESC" button:

    simple :: Reaction
    simple = proc ev -> do
        r <- integral -< 50
        displayAction <- arr (uncurry tag) <<< first redisplay -< (ev, actionIO . display $ r)
        reshapedAction <- arr (fmap (actionIO . reshape)) <<< reshaped -< ev
        returnA -< mconcat [reshapedAction, displayAction]
    
    anim :: Reaction
    anim = switch (simple &&& keyPressed) route
        where
            route (Left '\ESC') = arr (\_ -> Event actionExit)
            route _ = simple
    

    This seems to work fine. (Will edit with solution for the above)