Trapping getChar before echo

Hi, Is there any way of trapping keystrokes in Haskell, modifying them, and then echoing? Basically I want to give the user a prompt like:
and then have whatever they type appear in UPPERCASE regardless of whether caps lock was on or not. By default Haskell seems to echo characters in whatever case they were typed. I want to sneak in a toUpper in before the Chars get echoed. Thanks Mark Spezzano

Hi Mark,
http://haskell.org/hoogle/?hoogle=set+echo
Thanks, Neil
On Sun, Jan 31, 2010 at 8:47 AM, Mark Spezzano
Hi,
Is there any way of trapping keystrokes in Haskell, modifying them, and then echoing?
Basically I want to give the user a prompt like:
and then have whatever they type appear in UPPERCASE regardless of whether caps lock was on or not.
By default Haskell seems to echo characters in whatever case they were typed. I want to sneak in a toUpper in before the Chars get echoed.
Thanks
Mark Spezzano
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

import System.IO import Data.Char main = do hSetEcho stdin False hSetBuffering stdin NoBuffering hSetBuffering stdout NoBuffering scanLine where scanLine = do c <- hGetChar stdin putChar . toUpper $ c scanLine Am Sonntag, den 31.01.2010, 19:17 +1030 schrieb Mark Spezzano:
Hi,
Is there any way of trapping keystrokes in Haskell, modifying them, and then echoing?
Basically I want to give the user a prompt like:
and then have whatever they type appear in UPPERCASE regardless of whether caps lock was on or not.
By default Haskell seems to echo characters in whatever case they were typed. I want to sneak in a toUpper in before the Chars get echoed.
Thanks
Mark Spezzano
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Michael Hartl wrote:
import System.IO import Data.Char
main = do hSetEcho stdin False hSetBuffering stdin NoBuffering hSetBuffering stdout NoBuffering scanLine where scanLine = do c <- hGetChar stdin putChar . toUpper $ c scanLine
Last time I tried something like this [on Windows], it didn't seem to work. I wanted to trap arrow keys and so forth, but they seem to be being used for input history. (I.e., pressing the up-arrow produces previously-entered lines of text, and none of this appears to be reaching the Haskell program itself.) Has this changed since I tried it last year?

I've tried this example and it just lets me type in anything in CAPITALS, which is nice, but Delete key doesn't delete and the arrow keys unfortunately let me manoeuvre the cursor all over the screen. Also the biggest problem is that Enter doesn't terminate the input session. Isn't there a simple way to do something like this? Surely Haskell must have a standard getLine function that support CAPITALS and backspacing and no arrow keys. Arrows keys with history would be nice. Mark On 31/01/2010, at 11:27 PM, Andrew Coppin wrote:
Michael Hartl wrote:
import System.IO import Data.Char
main = do hSetEcho stdin False hSetBuffering stdin NoBuffering hSetBuffering stdout NoBuffering scanLine where scanLine = do c <- hGetChar stdin putChar . toUpper $ c scanLine
Last time I tried something like this [on Windows], it didn't seem to work. I wanted to trap arrow keys and so forth, but they seem to be being used for input history. (I.e., pressing the up-arrow produces previously-entered lines of text, and none of this appears to be reaching the Haskell program itself.) Has this changed since I tried it last year?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

It might be worth looking at something like a curses library.
On Mon, Feb 1, 2010 at 4:45 PM, Mark Spezzano
I've tried this example and it just lets me type in anything in CAPITALS, which is nice, but Delete key doesn't delete and the arrow keys unfortunately let me manoeuvre the cursor all over the screen. Also the biggest problem is that Enter doesn't terminate the input session.
Isn't there a simple way to do something like this?
Surely Haskell must have a standard getLine function that support CAPITALS and backspacing and no arrow keys. Arrows keys with history would be nice.
Mark
On 31/01/2010, at 11:27 PM, Andrew Coppin wrote:
Michael Hartl wrote:
import System.IO import Data.Char
main = do hSetEcho stdin False hSetBuffering stdin NoBuffering hSetBuffering stdout NoBuffering scanLine where scanLine = do c <- hGetChar stdin putChar . toUpper $ c scanLine
Last time I tried something like this [on Windows], it didn't seem to work. I wanted to trap arrow keys and so forth, but they seem to be being used for input history. (I.e., pressing the up-arrow produces previously-entered lines of text, and none of this appears to be reaching the Haskell program itself.) Has this changed since I tried it last year?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Last time I tried something like this [on Windows], it didn't seem to work. I wanted to trap arrow keys and so forth, but they seem to be being used for input history. (I.e., pressing the up-arrow produces previously-entered lines of text, and none of this appears to be reaching the Haskell program itself.) Has this changed since I tried it last year?
Doesn't work in windows, at least up till 6.10.1. There's a work-around though. {-# LANGUAGE ForeignFunctionInterface #-} import Data.Char import Control.Monad (liftM, forever) import Foreign.C.Types getHiddenChar = liftM (chr.fromEnum) c_getch foreign import ccall unsafe "conio.h getch" c_getch :: IO CInt main = do forever $ do c <- getHiddenChar putStrLn $ show (fromEnum c)

Tim Attwood wrote:
Last time I tried something like this [on Windows], it didn't seem to work. I wanted to trap arrow keys and so forth, but they seem to be being used for input history. (I.e., pressing the up-arrow produces previously-entered lines of text, and none of this appears to be reaching the Haskell program itself.) Has this changed since I tried it last year?
Doesn't work in windows, at least up till 6.10.1. There's a work-around though.
{-# LANGUAGE ForeignFunctionInterface #-}
import Data.Char import Control.Monad (liftM, forever) import Foreign.C.Types
getHiddenChar = liftM (chr.fromEnum) c_getch foreign import ccall unsafe "conio.h getch" c_getch :: IO CInt
main = do forever $ do c <- getHiddenChar putStrLn $ show (fromEnum c)
Thanks for the info. Does anyone know how this is related to the "haskeline" package on Hackage?

On Fri, Feb 5, 2010 at 10:41 AM, Andrew Coppin
Tim Attwood wrote:
Last time I tried something like this [on Windows], it didn't seem to work. I wanted to trap arrow keys and so forth, but they seem to be being used for input history. (I.e., pressing the up-arrow produces previously-entered lines of text, and none of this appears to be reaching the Haskell program itself.) Has this changed since I tried it last year?
Doesn't work in windows, at least up till 6.10.1. There's a work-around though.
{-# LANGUAGE ForeignFunctionInterface #-}
import Data.Char import Control.Monad (liftM, forever) import Foreign.C.Types
getHiddenChar = liftM (chr.fromEnum) c_getch foreign import ccall unsafe "conio.h getch" c_getch :: IO CInt
main = do forever $ do c <- getHiddenChar putStrLn $ show (fromEnum c)
Thanks for the info.
Does anyone know how this is related to the "haskeline" package on Hackage?
The haskeline package provides a readline-like library for reading in a line of input with arrow keys, tab completion, etc. It works on both Windows and unix platforms. Documentation and a full list of features can be found at http://trac.haskell.org/haskeline/ . On Windows, haskeline gets all user input by calling Win32 API functions such as ReadConsoleInputW: http://msdn.microsoft.com/en-us/library/ms684961%28VS.85%29.aspx That function returns an INPUT_RECORD struct with information about key press events (among others); those includes simple characters, arrow keys, page up/down, etc. AFAIK that's the only way to get at such events in the Windows console; there's no effective analogue to the unix setting, where e.g. pressing the up key causes stdin to receive the ANSI key sequence "\ESC[A". The source code of haskeline has examples of how to import and use those API functions: http://code.haskell.org/haskeline/System/Console/Haskeline/Backend/Win32.hsc Best, -Judah
participants (7)
-
Andrew Coppin
-
Judah Jacobson
-
Lyndon Maydwell
-
Mark Spezzano
-
Michael Hartl
-
Neil Mitchell
-
Tim Attwood