
I ran the following program:
module Main where
main = do putStrLn "Key" c <- getChar print c main
Unfortunately, it doesn't do what I was hoping. Specifically, it only returns any data when I hit [return]. It also seems to be giving me a command history somehow. Special keys (e.g., arrow keys, backspace, escape) don't get through to the application. I'm trying to write a program that uses individual keys as commands. What's the correct way to get this to work?

main = do hSetBuffering stdin NoBuffering; c <- getChar Your problem is that by default, POSIX environments use line buffering on stdin queuing up until it sees a carriage return before sending it to the
You want to use:
process. Which is in this case, your code.
-Edward Kmett
On Tue, Mar 31, 2009 at 3:38 PM, Andrew Coppin
I ran the following program:
module Main where
main = do putStrLn "Key" c <- getChar print c main
Unfortunately, it doesn't do what I was hoping. Specifically, it only returns any data when I hit [return]. It also seems to be giving me a command history somehow. Special keys (e.g., arrow keys, backspace, escape) don't get through to the application.
I'm trying to write a program that uses individual keys as commands. What's the correct way to get this to work?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Andrew Coppin wrote:
Edward Kmett wrote:
You want to use: > main = do hSetBuffering stdin NoBuffering; c <- getChar
Already tried that. It appears to make no difference.
Really? It fixes your first issue for me (data appears immediately when the key is pressed) but obviously does nothing to give you the special keys (ctrl, esc, etc) - not sure of a platform independent way to get that, but there's bound to be one. Are you on Windows? Did you try inserting a 'flush'?

On Tue, Mar 31, 2009 at 12:53 PM, Andrew Coppin
Edward Kmett wrote:
You want to use:
main = do hSetBuffering stdin NoBuffering; c <- getChar
Already tried that. It appears to make no difference.
Sounds like you're on Windows, so it's probably this bug: http://hackage.haskell.org/trac/ghc/ticket/2189 Although: if you're trying to get at the arrow keys from the Windows cmd console, it's not possible by reading stdin; rather, you need to access the lower-level Win32 console APIs: http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx Not sure if it will help, but you could take a look at what I did in Haskeline: http://code.haskell.org/haskeline/System/Console/Haskeline/Backend/Win32.hsc -Judah

Judah Jacobson wrote:
Sounds like you're on Windows, so it's probably this bug:
I am on Windows. It looks like my programs are running editline or something to give a command history. Ordinarily that would actually be quite useful, but for this particular application, not so much.
Although: if you're trying to get at the arrow keys from the Windows cmd console, it's not possible by reading stdin; rather, you need to access the lower-level Win32 console APIs:
http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx
Not sure if it will help, but you could take a look at what I did in Haskeline:
http://code.haskell.org/haskeline/System/Console/Haskeline/Backend/Win32.hsc
All useful ideas. I'll give that a shot...

On Tue, Mar 31, 2009 at 2:11 PM, Andrew Coppin
Judah Jacobson wrote:
Sounds like you're on Windows, so it's probably this bug:
I am on Windows. It looks like my programs are running editline or something to give a command history. Ordinarily that would actually be quite useful, but for this particular application, not so much.
Actually, when you read a buffered line of input from the Windows console it automatically provides line history for the user. (This is Win32 behavior, nothing to do with Haskell or editline.) As you say, sometimes it's more useful than others. -Judah

Judah Jacobson wrote:
Not sure if it will help, but you could take a look at what I did in Haskeline:
http://code.haskell.org/haskeline/System/Console/Haskeline/Backend/Win32.hsc
It would be nice to get some of that into the main win32 package... Cheers, Simon
participants (5)
-
Andrew Coppin
-
Edward Kmett
-
Judah Jacobson
-
Simon Marlow
-
Thomas DuBuisson