TurionAltec Posted May 31, 2008 Share Posted May 31, 2008 (edited) 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 September 28, 2009 by TurionAltec Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 31, 2008 Share Posted May 31, 2008 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 More sharing options...
TurionAltec Posted May 31, 2008 Author Share Posted May 31, 2008 So no way to easily link the keyboard input in a "DOS box" to STDIN? Sigh, I guess I'll try a workaround like listening on a local TCP port and telneting in. Link to comment Share on other sites More sharing options...
TurionAltec Posted September 28, 2009 Author Share Posted September 28, 2009 Found a solution to this. Using FileOpen("con") http://www.autoitscript.com/forum/index.php?showtopic=79275&p=571658 Got there from this post: http://www.autoitscript.com/forum/index.php?showtopic=103132&view=findpost&p=731004 Link to comment Share on other sites More sharing options...
carstengiese Posted July 18, 2019 Share Posted July 18, 2019 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 More sharing options...
carstengiese Posted July 18, 2019 Share Posted July 18, 2019 ...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 More sharing options...
Developers Jos Posted July 18, 2019 Developers Share Posted July 18, 2019 @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 More sharing options...
Bidder Posted April 22, 2022 Share Posted April 22, 2022 @Jos It may be that the answer belongs in a new thread, but it was exactly what I was looking for and is a better, simpler answer. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now