Jump to content
Sign in to follow this  
paulpmeier

Console Input

Recommended Posts

paulpmeier

A usefull feature of the FileOpen function:

FileOpen("con", 4) opens console for binary input.

#AutoIt3Wrapper_Change2CUI=y

$input = ""
ConsoleWrite(@CRLF & "Write some text and close with ENTER: ")

$file = FileOpen("con", 4)
While 1
    $chr = FileRead($file, 1)
    If $chr = @LF Then ExitLoop
    $input = $input & BinaryToString($chr)
WEnd
FileClose($file)

ConsoleWrite(@CRLF & "Input was: " & $input & @CRLF)

To read lines instead of single characters use this code:

#AutoIt3Wrapper_Change2CUI=y

$maxchr = 123
$input = ""
ConsoleWrite(@CRLF & "Write some text and close with ENTER: ")

$file = FileOpen("con", 4)
$line = BinaryToString(FileRead($file, $maxchr))
FileClose($file)

ConsoleWrite(@CRLF & "Your input was: " & $line & @CRLF)

Compile as CUI and start the exe-file from console prompt.

(Tested with AutoIt 3.2.12.1 and Windows XP SP2)

Paul

See also: Reading from serial ports with FileRead

Edited by paulpmeier

Share this post


Link to post
Share on other sites
ResNullius

A usefull feature of the FileOpen function:

FileOpen("con", 4) opens console for binary input.

#AutoIt3Wrapper_Change2CUI=y

$input = ""
ConsoleWrite(@CRLF & "Write some text and close with ENTER: ")

$file = FileOpen("con", 4)
While 1
    $chr = FileRead($file, 1)
    If $chr = @CR Then ExitLoop
    $input = $input & BinaryToString($chr)
WEnd
FileClose($file)

ConsoleWrite(@CRLF & "Input was: " & $input & @CRLF)

Compile as CUI and start the exe-file from console prompt.

(Tested with AutoIt 3.2.12.1 and Windows XP SP2)

Paul

Excellent find Paul!

Out of curiosity, did you have this knowledge from another programming language, or did you just reason it out?

Share this post


Link to post
Share on other sites
paulpmeier

Hello ResNullius

I remembered a similar old Microsoft Basic statement.

So I tried it with the AutoIt FileRead function.

And it works.

Paul

Share this post


Link to post
Share on other sites
Nahuel

That's a great example. ;) Thank you.

Share this post


Link to post
Share on other sites
Cyber

FANTASTIC! THANKS!! :)


Console Browse: Navigate on the WEB in a textual consoleMultiPing!: Show computer on the lan and/or show the local/remote task, ALL animated!KillaWin: Event executingCryptPage: Crypt your webpage and show only with key

Share this post


Link to post
Share on other sites
sniper120

#AutoIt3Wrapper_Change2CUI=y
   
   $input = ""
   ConsoleWrite(@CRLF & "Write some text and close with ENTER: ")
   
   $file = FileOpen("con", 4)
   While 1
       $chr = FileRead($file, 1)
       If $chr = @LF Then ExitLoop
       $input = $input & BinaryToString($chr)
   WEnd
   FileClose($file)
   
   ConsoleWrite(@CRLF & "Input was: " & $input & @CRLF)
:party:

There is an error with the output! A Line Feed (@LF) is placed at the end of the string. This screwed my code up! Here is the fix:

ConsoleWrite(@CRLF & "Messge: ")
 $file = FileOpen("con", 4)
 While 1
     $chr = FileRead($file, 1)
     If $chr = @LF Then ExitLoop
     $pin = $pin & BinaryToString($chr)
 WEnd
 FileClose($file)
 $pin = StringStripCR($pin); <-- Fix

:) Look at the last line of the code.

Share this post


Link to post
Share on other sites
ResNullius

:)

There is an error with the output! A Line Feed (@LF) is placed at the end of the string. This screwed my code up! Here is the fix:

Funny, but I wouldn't always get the extra @LF.

Nonetheless, I think this is probably the better fix:

#AutoIt3Wrapper_Change2CUI=y

$input = ""
ConsoleWrite(@CRLF & "Write some text and close with ENTER: ")

$file = FileOpen("con", 4)
While 1
    $chr = FileRead($file, 1)
    If $chr = @CR Or $chr = @LF Then ExitLoop; <------- Added check for @CR as well as @LF
    $input = $input & BinaryToString($chr)
WEnd
FileClose($file)
ClipPut($input & "NewLine")
ConsoleWrite(@CRLF & "Input was: " & $input & @CRLF)
Edited by ResNullius

Share this post


Link to post
Share on other sites
GaryFrost

Funny, but I wouldn't always get the extra @LF.

Nonetheless, I think this is probably the better fix:

#AutoIt3Wrapper_Change2CUI=y

$input = ""
ConsoleWrite(@CRLF & "Write some text and close with ENTER: ")

$file = FileOpen("con", 4)
While 1
    $chr = FileRead($file, 1)
    If $chr = @CR Or $chr = @LF Then ExitLoop; <------- Added check for @CR as well as @LF
    $input = $input & BinaryToString($chr)
WEnd
FileClose($file)
ClipPut($input & "NewLine")
ConsoleWrite(@CRLF & "Input was: " & $input & @CRLF)

Don't think I would exit the loop, rather continueloop, might be a lf or cr in between other lines.


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites
ResNullius

Don't think I would exit the loop, rather continueloop, might be a lf or cr in between other lines.

Well, the OP's original function appeared to be just to capture a single input line from the console.

So looking for a lf/cr seems the most logical to indicate end of that single line, no?

Agreed that multi-line input would call for a different approach to signal end of input, like the old CTRL-Z ("^Z") from "Copy CON" days.

But again, for single line input isn't exiting the loop most appropriate?

Share this post


Link to post
Share on other sites
Kealper

wow just found this topic, very very cool, thanks alot!

but i've got a question...i've been fooling around with some things to no avail, any thoughts on how do clear all data from the console (like CLS for batch), and i've seen programs like AlacrityPC use different text colors for different lines of text in it's console (like an error or warning might be a yellow text, while normal is light gray, and debug info is dark gray)...any ideas how they did that? lol


Share this post


Link to post
Share on other sites
ResNullius

wow just found this topic, very very cool, thanks alot!

but i've got a question...i've been fooling around with some things to no avail, any thoughts on how do clear all data from the console (like CLS for batch), and i've seen programs like AlacrityPC use different text colors for different lines of text in it's console (like an error or warning might be a yellow text, while normal is light gray, and debug info is dark gray)...any ideas how they did that? lol

Check out this thread for some ideas on overwriting console output and a coloured text example in Post # 9 by The Kandie Man

Also, for clearing the entire console see rover's work here:

http://www.autoitscript.com/forum/index.php?s=&showtopic=92049&view=findpost&p=662228

or monoceres's here:

http://www.autoitscript.com/forum/index.php?s=&showtopic=91557&view=findpost&p=659264

Share this post


Link to post
Share on other sites
cherdeg

BTW: none of the variants here does exitloop for me on pressing ENTER.

Share this post


Link to post
Share on other sites
ResNullius

BTW: none of the variants here does exitloop for me on pressing ENTER.

Since AutoIt has dropped support for opening files in RAW mode, the method for doing this now involves _WinApi functions.

See #761205 for an updated version.

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
Sign in to follow this  

×