Basically you have to do two things to achieve your goal:
You need to disable any buffering in the terminal so that your program immediately presses the keys (and you do not need to confirm with ENTER).
hSetBuffering stdin NoBuffering
Then you must make sure that the character is not recalled back to the terminal.
hSetEcho stdin False
Please note that before exiting your program, it must restore the previous settings in order to allow further use of the terminal.
The code combination will look like this:
import System.IO import Control.Exception (bracket) withHiddenTerminalInput :: IO a -> IO a withHiddenTerminalInput io = bracket (do prevBuff <- hGetBuffering stdin prevEcho <- hGetEcho stdin hSetBuffering stdin NoBuffering hSetEcho stdin False return (prevBuff, prevEcho) ) (\(prevBuff, prevEcho) -> do hSetBuffering stdin prevBuff hSetEcho stdin prevEcho ) (const io)
source share