francoiste

ConsoleRead() not working

4 posts in this topic

#1 ·  Posted (edited)

i'm writing a command line tool (i.e. CUI instead of GUI) and would like to query the user for input, such as:

  • Press any key to continue
  • Do you really want to proceed? [y|n]

i'm using the following code (derived from the example in the help file).

but it does not wait for user input. it continues immediately.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

AutoItSetOption("MustDeclareVars", 1)
Opt("TrayAutoPause", 0)

If Not @Compiled Then
    ConsoleWrite("ERROR - This script must be compiled to EXE in order to read from the console." & @CRLF)
    ConsoleWrite("exiting ..." & @CRLF)
    Sleep(5000)
    Exit (1)
EndIf

ConsoleWrite("Do you want to continue? [y|n]" & @CRLF)

Local $sOutput = ""
While True
    $sOutput &= ConsoleRead()
    If @error Then ExitLoop
    Sleep(25)
WEnd

If $sOutput <> "y" Then
    ConsoleWrite("skipping - your answer was: " & $sOutput & @CRLF)
Else
    ConsoleWrite("proceeding - your answer was: " & $sOutput & @CRLF)

    ; do some funny stuff ...
    Sleep(5000)
EndIf

ConsoleWrite("done. exiting ..." & @CRLF)
Sleep(5000)

 

am i missing something?

Edited by francoiste

Share this post


Link to post
Share on other sites



sure i have compiled it. see the code snippet i have provided in my initial question.

the help file looks a bit strange. the comments are giving an example to invoke it on a command prompt something like "echo Hello! | ConsoleRead.exe" - so it is piped to STDIN. that seems to be a whole different story.

but when writing a console tool we obviously need some simple way to do both, ConsoleWrite() and ConsoleRead().
is such functionality not available out-of-the-box?
reading through other discussions it seems this was only possible by using custom UDF, such as "Console.au3" (by Shaggi)?

see this one, for exmple:

 

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I ran the sample read code in the help file and it ran perfectly.  EDIT: I need to do some testing on my side for the write ... 

Edit 2: I am not an expert on console applications so that UDF might be the best way to go but I can't say for sure.  It seems like you can get there a few different ways with DLL calls, the WinAPI, or a pre-assembled UDF but I don't know that there is an easy out of the box function.  All threads seem to point to what you found (console.au3).   Not sure if someone else wants to jump in with a better approach ... 

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

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