1

I am writing a simple text editor, so I want to have something like this

type Scancode = Int
data KeyState = Pressed | Released
newtype InStream = InStream [(Scancode, State)]

main = do
    input <- getKeys
    parse input

parse :: InStream -> IO ()
parse [] = return ()
parse (x : xs)
  | x == (1, Released) = return ()
  | otherwise = do
      doSomething
      parse xs

As you could guess, I want getKeys function to behave like getContents, to have continuos list of scancodes.

As I know SDL or even GTK can provide me such functionality, but is there more idiomatic (for haskell and functional programming at all) and with less "overhead" way to do such thing?

P.S. If it matters, I want to use my "editor" under Linux both in console (tty) and X11/Wayland.

Vanzef
  • 453
  • 1
  • 5
  • 17
  • AFAIK there is standard library way to get access to input. Also note that getContents (and by extension, interact and friends) are usually not used that much because lazy IO has a tendency to be a bit of a pain in the ass. – Cubic Aug 26 '15 at 21:10
  • @Cubic Could you please tell me more about lazy IO and "pain in the ass"? I am new in haskell and programming at all, so I don't know this. – Vanzef Aug 26 '15 at 21:40
  • lazy IO isn't too bad. It just doesn't work like you would expect. – PyRulez Aug 26 '15 at 21:48

1 Answers1

1

If you really want simple, then check out these answers:

You might have to put your tty into raw mode first for it to work. The second question asks for a Windows solution, but the same idea should also work for Linux.

Community
  • 1
  • 1
ErikR
  • 51,541
  • 9
  • 73
  • 124