Haskell的SDL库中的操纵杆事件处理

时间:2014-03-14 23:46:41

标签: haskell sdl joystick

我正在尝试编写一个小程序,使用Haskell SDL绑定检测视频游戏控制器上的按钮输入。我的程序很简单:

import Control.Monad (forever, when)
import System.Exit (exitSuccess)
import Data.Time.Clock.POSIX (getPOSIXTime)

import qualified Graphics.UI.SDL.General  as SG
import qualified Graphics.UI.SDL.Events   as SE
import qualified Graphics.UI.SDL.Joystick as SJ
import qualified Graphics.UI.SDL.Video    as SV
import qualified Graphics.UI.SDL.Types    as ST


main :: IO ()
main =
  SG.withInit [SG.InitVideo, SG.InitJoystick] $ do
    -- Open joystick.  We assume it's always present.
    numJoysticks <- SJ.countAvailable
    putStrLn $ show numJoysticks ++ " joystick(s) available"
    js <- SJ.open 0

    -- Create window
    SV.setVideoMode 320 240 24 [ST.SWSurface, ST.Resizable]

    -- Handle events
    forever $ do
      evt <- SE.waitEvent
      case evt of
        SE.JoyButtonDown dev btn -> do
                 t <- getPOSIXTime
                 print (show t ++ " - Button pressed: " ++ show dev ++ " " ++ show btn)
        SE.Quit -> exitSuccess
        _ -> return ()

在我的系统上(Debian stable,GHC 7.4,libghc-sdl-dev 0.6.3),按钮事件在程序执行的第一秒(或多或少)期间被捕获,然后什么都没有。我的代码有问题吗?

1 个答案:

答案 0 :(得分:1)

SJ.close js阻止后添加forever。我相信当Haskell认为不再需要时,操纵杆会自动关闭。