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

using Backspace to delete last typed letter

Started by Goof, 04 January 2013 - 10:59 PM
Goof #1
Posted 04 January 2013 - 11:59 PM
Hi Again…


I am trying to make a input terminal(not really)
i want it to listen for 9 letters, which it would wrote down the terminal as they come…
But… i cant figure out how to use the Backspace to delete the last letter that was typed…

Spoiler


tP(25,7)
tB(colors.blue)
tT(colors.white)

print("		 ") -- password place...

tx,ty  = 25,7
   for i = 1, 9 do
	  local event, arg = os.pullEvent("char")
		 if event == "char" then
			   if arg == 14 or arg == keys.backspace then -- if i press backspace it SHOULD do the following, ( but it seems like it does not go into this if statement.. )
				  tP(tx-1,ty)
				  write(" ")
				  i = i - 1
			   else -- if not a backspace pressed, then input the letter...

				  tP(tx,ty)
				  write(arg)
				  table.insert(eTPassword, arg)
			  end
		 end
	  tx = tx + 1
   end

for j = 1, #eTPassword do -- this is to input the whole password ( when done ) to 1 variable.. ( please help me with this too ) ( it is inputting every letters that i've typed in, into a table ( eTPassword ) but i dont --	  

-- know how to insert all the letters from the table to 1 single variable.


end



Thanks in advance
remiX #2
Posted 05 January 2013 - 12:05 AM
You're pulling a char event, which you should not.

Do not pull an event in os.pullEvent() and then do this:


if event == "key" then
	if arg == keys.backspace then
		--
	elseif arg == keys.enter then
		table.insert(eTPassword, arg) -- Now insert it into the table after clicking enter

elseif event == "char" then
	write(arg)
	-- table.insert(eTPassword, arg)
	-- I'd suggest to only add the password into the table after the person clicks enter.
end

Also, why are you using a for loop?

EDIT: Heres a custom function I made to maximize the amount of characters allowed in the read.

function limitRead(nLimit, replaceChar)
    term.setCursorBlink(true)
    local cX, cY = term.getCursorPos()
    local rString = ""
    if replaceChar == "" then replaceChar = nil end
    repeat
        local event, p1 = os.pullEvent()
        if event == "char" then
            -- Character event
            if #rString + 1 <= nLimit then
                rString = rString .. p1
                write(replaceChar or p1)
            end
        elseif event == "key" and p1 == keys.backspace and #rString >= 1 then
            -- Backspace
            rString = string.sub(rString, 1, #rString-1)
            xPos, yPos = term.getCursorPos()
            term.setCursorPos(xPos-1, yPos)
            write(" ")
            term.setCursorPos(xPos-1, yPos)
        end
    until event == "key" and p1 == keys.enter
    term.setCursorBlink(false)
    print() -- Skip to the next line after clicking enter.
    return rString
end

-- And you call it like this
input = limitRead(10)

-- If you want to replace it with a char, like read("*") then
password = limitRead(10, "*")
Goof #3
Posted 05 January 2013 - 12:25 AM
but, Why are you using 2 term.getCursorPos() ??
remiX #4
Posted 05 January 2013 - 12:44 AM
For when it backspaces? Because when it does write(" ") it changes the cursor position back to it's original so it needs to go back again.
Goof #5
Posted 05 January 2013 - 12:50 AM
ahh… (i feel like im stupid) :(/>


thanks
KaoS #6
Posted 05 January 2013 - 01:52 AM
remiX: you do not have to include the full #str-1 in string.sub


rString = string.sub(rString, 1, #rString-1)
can just be

rString = string.sub(rString, 1, -1)
Goof #7
Posted 05 January 2013 - 01:56 AM
But, KaoS.
it still works, with the

rString = string.sub(rString, 1, #rString-1)
and wasnt the most important thing, to get this code to work? yes.

Thanks to you guys, anyway :D/>
remiX #8
Posted 05 January 2013 - 01:57 AM
Oh, good to know. I haven't used string api much myself… but how does that work, Kaos?

If the number is negative does it start from the end of the string?
KaoS #9
Posted 05 January 2013 - 02:07 AM
If the number is negative does it start from the end of the string?

exactly. it is quite a nice way of doing it I think. most languages work that way I think, I know VBS and JS do


Thanks to you guys, anyway :D/>

hehe I did nothing. props to remiX
Goof #10
Posted 05 January 2013 - 02:29 AM
Yeah :D/>

but thanks :D/> (you did help remiX) :D/>