Jump to content

Console Input


paulpmeier
 Share

Recommended Posts

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
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • 4 months later...
  • 3 months later...

#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.

Link to comment
Share on other sites

:)

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
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • 7 months later...

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 months 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...