This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
lieudusty's profile picture

Existing Text in read()?

Started by lieudusty, 19 October 2012 - 10:07 PM
lieudusty #1
Posted 20 October 2012 - 12:07 AM
Hi! I'm trying to get a text field (read()) to have something already there before the user inputs any text. Does anyone know how to do this?
PixelToast #2
Posted 20 October 2012 - 12:10 AM
modify the sLine variable at the top of the read function
faubiguy #3
Posted 20 October 2012 - 12:12 AM
You can do
os.queueEvent("char", "Starting text")
input = read()

This has the side effect of the starting cursor in read still being after the first character
EDIT: It doesn't if you put os.queueEvent("key", keys["end"]) between where you queue the message and read()
Espen #4
Posted 20 October 2012 - 12:23 AM
You could first write something infront of it like so:

write("Enter Password: ")
local input = read()

And for convenience sake you can put it into a function:

local function textRead( text )
  write( text )
  return read()
end

This way you can comfortably use textRead() whenever you want to write something before an input:

local pwd = textRead("Please Enter your Password: ")
brett122798 #5
Posted 20 October 2012 - 03:56 AM
Well, here's what you might do:


prevtext = "What's your name? | Answer: "
write(prevtext)
aftertext = read()
aftertext = (prevtext..aftertext)

I think the last line will work..
lieudusty #6
Posted 20 October 2012 - 04:33 AM
Well I wanted the text there that you can edit the existing text too.
faubiguy #7
Posted 20 October 2012 - 05:46 AM
My suggestion will do that. It makes it so that read will pull the starting text queued as a char event and put it there before you start writing.
lieudusty #8
Posted 20 October 2012 - 06:29 PM
My suggestion will do that. It makes it so that read will pull the starting text queued as a char event and put it there before you start writing.
I tried your solution it works but were right, the cursor starts at the second character
faubiguy #9
Posted 20 October 2012 - 06:30 PM
Use os.queueEvent("key", keys["end"]) after queueing the message but before calling read()
Cloudy #10
Posted 20 October 2012 - 06:47 PM
Why don't you just copy read and make your own function? It is open source in bios.lua. Should be trivial to prefill the character buffer.

Edit: as was suggested by pixeltoast.
Lyqyd #11
Posted 21 October 2012 - 12:22 AM
read() can be passed a history table. Passing a table containing the string and queuing an up-arrow key event may do what you wish. Copying read() and adding this (very simple) feature is certainly the best way to do this, though.