Jump to content




using Backspace to delete last typed letter


  • You cannot reply to this topic
9 replies to this topic

#1 Goof

  • Members
  • 751 posts

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


Thanks in advance

#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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, "*")


#3 Goof

  • Members
  • 751 posts

Posted 05 January 2013 - 12:25 AM

but, Why are you using 2 term.getCursorPos() ??

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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.

#5 Goof

  • Members
  • 751 posts

Posted 05 January 2013 - 12:50 AM

ahh... (i feel like im stupid) :(


thanks

#6 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

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)


#7 Goof

  • Members
  • 751 posts

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

#8 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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?

#9 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 05 January 2013 - 02:07 AM

View PostremiX, on 05 January 2013 - 01:57 AM, said:

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


View PostMikk809h, on 05 January 2013 - 01:56 AM, said:

Thanks to you guys, anyway :D

hehe I did nothing. props to remiX

#10 Goof

  • Members
  • 751 posts

Posted 05 January 2013 - 02:29 AM

Yeah :D

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





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users