Haskell: read input character from console immediately, not after newline

前端 未结 4 425
暗喜
暗喜 2020-12-09 08:17

I\'ve tried this:

main = do
    hSetBuffering stdin NoBuffering 
    c <- getChar

but it waits until the enter is pressed, which is not

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 08:56

    The Haskeline package worked for me.

    If you need it for individual characters, then just change the sample slightly.

    1. getInputLine becomes getInputChar
    2. "quit" becomes 'q'
    3. ++ input becomes ++ [input]
    main = runInputT defaultSettings loop
        where 
            loop :: InputT IO ()
            loop = do
                minput <- getInputChar "% "
                case minput of
                    Nothing -> return ()
                    Just 'q' -> return ()
                    Just input -> do outputStrLn $ "Input was: " ++ [input]
                                     loop
    

提交回复
热议问题