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

Selecting text input?

Started by lieudusty, 14 August 2012 - 03:49 AM
lieudusty #1
Posted 14 August 2012 - 05:49 AM
Hi everyone =D

I'm making a program and I need to select text boxes. So like there is a field that says username and under it says password. I need it to be able to use tab/arrow keys to switch between the different fields. Can someone please help me with this? Thanks!
ardera #2
Posted 14 August 2012 - 07:21 AM
Code:
Spoiler

local username=""
local pw=""
local sel=1
while true do
  local function act() --actualize
    term.clear()
    term.setCursorPos(1, 1)
    write("Username: ")
    write(username)
    term.setCursorPos(1, 2)
    write("PW")
    write(string.rep("*", string.len(pw))
    term.setCursorPos(1, 18)
    term.write("Press Tab to select and enter to contine") --sometimes write scrolls the terminal if you set the Cursor Pos to 1, 18 and write there.
  end
  event, p1=os.pullEvent()
  if event=="key" then
    if p1==15 then
	  if sel==1 then sel=2
	  elseif sel==2 then sel=1 end
    elseif p1==28 then
	  break --The Username is in the variable "username" and the pw
	  --in "pw"
    elseif p1==14 then
	  if sel==1 then
	    if string.len(username)>1 then
		  username=string.sub(username, 1, -2)
	    elseif string.len(username)==1 then username=1 end
	  elseif  sel==2 then
	    if string.len(pw)>1 then
		  pw=string.sub(pw, 1, -2)
	    elseif string.len(pw)==1 then
		  pw=""
	    end
	  end  
    end
  elseif event=="char" then
    if sel==1 then
	  username=username..p1
    elseif sel==2 then
	  pw=pw..p1
    end
  end
  if p1==0 then --there is a param 0 in the "key" event (sometimes). That don't means a key is pressed.
    --(make a while loop which prints events and params then you know what I mean)
    if event≃"key" then
	  act()
    end
  else
    act()
  end
end
Press Tab to select and Enter to contine.
Didn't tested it, (today Im using Linux) But I think it should Work.