Jump to content

(solved) Problems using ConsoleRead() to read input


Recommended Posts

I'm trying to make a command line utility that will read input from the console that it was called from, and write output back to the console.

I have the following code:

;Console.au3
;Reads console input and outputs message box
#AutoIt3Wrapper_Change2CUI=Y

ConsoleWrite("Please enter data:")
Sleep(6000)
$console = ConsoleRead()
$buttoncode = MsgBox(2, "From console I read", $console)
If $buttoncode = 3 Then
    $button = "abort"
ElseIf $buttoncode = 4 Then
    $button = "retry"
ElseIf $buttoncode = 5 Then
    $button = "ignore"
EndIf
ConsoleWrite(@CRLF & "The " & $button & " button was pressed in the Msgbox." & @CRLF)

When run from Scite it runs as expected. In the Output Pane at the bottom of Scite "Please enter data:" appears, any text typed within 6 seconds will appear in the Msgbox, and the pressed msgbox button will appear in the Output pane.

When compiled to an .exe, the program will accept input only if piped into it.

Echo test message | console.exe

If the program is just launched as console.exe, and keys are typed on the keyboard, the Msgbox will appear blank, and pressed keys are passed back to cmd when the script terminates.

Is there anyway to get ConsoleRead() to read keyboard input?

Edited by TurionAltec
Link to comment
Share on other sites

The command shell "DOS Box" is not a "console" in the meaning of ConsoleRead() and ConsoleWrite(). As you found, the console at the bottom of SciTE is a "console" for this purpose.

When you compile as a console (non-GUI) application, ConsoleRead() connects to STDIN and ConsoleWrite() connects to STDOUT. Now you can see ConsoleWrite() data through STDOUT show up on the "DOS Box", but what is typed in there does not go to STDIN for ConsoleRead() to see unless you ECHO it or pipe it there.

Confusing, ain't it?

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 1 year later...
  • 9 years later...

You were assuming, that "ConsoleRead()" waits till you press any key, but this is NOT the case.
Even if you do not enter anything, it will continue with an empty output.

Here is a function that reads input from the console:

func ReadInput() ; read input from STDIN till two line-breaks are entered
    Local $sResult
    Local $sText
    While True
        $sText = ConsoleRead()
        If @error Then ExitLoop
        if $sText<>"" then
            if $sText=@CRLF then
                ExitLoop
            Else
                $sResult &= $sText
            EndIf
        EndIf
        Sleep(20)
    WEnd
    return $sResult
EndFunc

Link to comment
Share on other sites

...and in case you just want to read till a single line-break, then use this:

func ReadLine()
    Local $sResult
    Local $sText
    While True
        $sText = ConsoleRead()
        If @error Then ExitLoop
        if StringRight($sText, 1) = @LF then
            $sResult &= $sText
            ExitLoop
        EndIf
        Sleep(20)
    WEnd
    return $sResult
EndFunc

Link to comment
Share on other sites

  • Developers

@carstengiese,

Welcome to our forums, but .....
Do you realised you just answered a "SOLVED" marked 10 years old topic?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...