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

Text Input & Buttons!

Started by hbomb79, 18 September 2014 - 07:26 AM
hbomb79 #1
Posted 18 September 2014 - 09:26 AM
How would I go about the following:

A setup page that asks you for an input, when entered you click a certain button which runs a function. the function obtains what you typed in the text box and then does what ever I choose (Not really important)


I want this:
- Page Opens
- Asks for input
- When finished imputing, click button and stores what was typed in a variable

Ive mucked around and got the buttons working… But the text box im not to sure about, I thought using parallel, but how would I get the input?
Edited on 18 September 2014 - 07:42 AM
KingofGamesYami #2
Posted 18 September 2014 - 01:22 PM
I'd have a custom read function for the box running in parallel with the button, and when finished, dumps the input into a variable. Something like:

local input --#input box dumps the data into this variable
parallel.waitForAny( button, inputBox )

A suggestion: Have your inputBox function return when it receives a "return_input" event. Then have your button function do

os.queueEvent( "return_input" )
when clicked.
hbomb79 #3
Posted 18 September 2014 - 09:43 PM
That's a good suggestion, but it's the custom read function I can't get my head around… How would I make it return when that event is queued?
KingofGamesYami #4
Posted 19 September 2014 - 12:24 AM
I don't happen to have a custom read for a input box lying around, I thought you had one… you could try and find one, I'm sure someone has posted one.
Bomb Bloke #5
Posted 19 September 2014 - 01:45 AM
Assuming your "read" function has a line like this somewhere:

if event == "key" and par1 == keys.enter then

… you'd change it to:

if (event == "key" and par1 == keys.enter) or event == "return_input" then

Using such an event may or may not be the best way to go about it, depending on the code you've got so far.
hbomb79 #6
Posted 19 September 2014 - 05:20 AM
Well, I have no code so far… Ill try to think of something
hbomb79 #7
Posted 19 September 2014 - 06:24 AM
Thanks Guys, Heres what I managed to get working, for anyone else interested:

Read Function:

function readN(len, replaceChar)
  submitted = false
  term.setTextColor(1)
  len = len or 10
  local input=""
  local key = 0
  term.setCursorBlink(true)
  repeat
	    event, p1, p2, p3 = os.pullEvent()
	    if event=="char" then
		  if #input < len then
   input = input .. p1
   term.write(replaceChar or p1)
		  end
	    elseif event=="key" and p1==keys.backspace and #input > 0 then
		  input = input:sub(1,#input-1)
		  local x,y = term.getCursorPos()
		  term.setCursorPos(x-1,y)
		  term.write(" ")
		  term.setCursorPos(x-1,y)
	    elseif event == "submit_Result" then --Button Event
   submitted = true
   return input
  elseif event == "mouse_click" then
   doClick('mouse_click', nil, p2, p3)
  end
  until submitted
end

Call Code:

input = readN(20)