nntpnews.net

Global Usenet Archiver


Register

[Haskell-cafe] Trapping getChar before echo

Reply

  #1  
Old 31-01-10, 08:48 AM
Mark Spezzano
 
Posts: n/a
Default [Haskell-cafe] 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


_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #2  
Old 31-01-10, 10:05 AM
Neil Mitchell
 
Posts: n/a
Default Re: [Haskell-cafe] Trapping getChar before echo

Hi Mark,

Code:
Content visible to registered users only.
Thanks, Neil

On Sun, Jan 31, 2010 at 8:47 AM, Mark Spezzano
< - > wrote:
> 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
>
Code:
Content visible to registered users only.
>
Code:
Content visible to registered users only.
>

_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #3  
Old 31-01-10, 10:13 AM
Michael Hartl
 
Posts: n/a
Default Re: [Haskell-cafe] Trapping getChar before echo

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
>
Code:
Content visible to registered users only.
>
Code:
Content visible to registered users only.



_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #4  
Old 31-01-10, 12:58 PM
Andrew Coppin
 
Posts: n/a
Default Re: [Haskell-cafe] Trapping getChar before echo

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
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #5  
Old 01-02-10, 08:45 AM
Mark Spezzano
 
Posts: n/a
Default Re: [Haskell-cafe] Trapping getChar before echo

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
>
Code:
Content visible to registered users only.
>
Code:
Content visible to registered users only.
>
>


_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #6  
Old 01-02-10, 09:14 AM
Lyndon Maydwell
 
Posts: n/a
Default Re: [Haskell-cafe] Trapping getChar before echo

It might be worth looking at something like a curses library.

On Mon, Feb 1, 2010 at 4:45 PM, Mark Spezzano
< - > wrote:
> 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
>>
Code:
Content visible to registered users only.
>>
Code:
Content visible to registered users only.
>>
>>

>
> _______________________________________________
> Haskell-Cafe mailing list
>
Code:
Content visible to registered users only.
>
Code:
Content visible to registered users only.
>

_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #7  
Old 02-02-10, 01:47 AM
Tim Attwood
 
Posts: n/a
Default [Haskell-cafe] Re: Trapping getChar before echo

> 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)


_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #8  
Old 05-02-10, 06:41 PM
Andrew Coppin
 
Posts: n/a
Default Re: [Haskell-cafe] Re: Trapping getChar before echo

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?

_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #9  
Old 05-02-10, 11:39 PM
Judah Jacobson
 
Posts: n/a
Default Re: [Haskell-cafe] Re: Trapping getChar before echo

On Fri, Feb 5, 2010 at 10:41 AM, Andrew Coppin
< - > wrote:
> 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
Code:
Content visible to registered users only.
.

On Windows, haskeline gets all user input by calling Win32 API
functions such as ReadConsoleInputW:

Code:
Content visible to registered users only.
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:
Code:
Content visible to registered users only.
Best,
-Judah
_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Haskell-beginners] getChar and keyboard reading Matthias Güdemann fa.haskell 2 24-01-10 09:08 PM
[Haskell-cafe] [Haskell Cafe] ASCII Graphics Library wanted Paul Sujkov fa.haskell 3 30-08-09 11:07 PM
[Haskell-cafe] Re:Haskell-Cafe Digest, Vol 71, Issue 30 silent_stream fa.haskell 2 25-07-09 12:09 AM
[Haskell-cafe] Re: Haskell-Cafe Digest, Vol 63, Issue 23 John Lato fa.haskell 1 15-11-08 01:13 AM
[Haskell-cafe] Re: Haskell-Cafe Digest, Vol 61, Issue 49 Mark Snyder fa.haskell 1 28-09-08 03:50 PM


All times are GMT +1. The time now is 11:59 PM. Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0



For ads on this site use independent advertising companies. These companies may use some data (which does not include your name, address, email address or telephone number) about your visits to this and other websites to create advertisements on products and services you might enjoy. If you'd like more information and to know the options available to prevent the use of such information by these companies, click here

Abuse Ticket System