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

[SOLVED] [Question] How to limit writed characters by user?

Started by Agenerick, 16 January 2013 - 09:05 AM
Agenerick #1
Posted 16 January 2013 - 10:05 AM
I was writing an password program but anyone can write longer than 9 characters because I writed a nice gui but the characters will replace the characters from gui, so how I can limit characters writed by USER to for example 9?
remiX #2
Posted 16 January 2013 - 10:06 AM
Here's a limit read functinn


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(9)

-- If you want to replace it with a char, like read("*") then
password = limitRead(9, "*")
Cranium #3
Posted 16 January 2013 - 10:08 AM
Ha! I made a very similar limitRead() function.
remiX #4
Posted 16 January 2013 - 10:10 AM
Ha! I made a very similar limitRead() function.

Was it also called limitRead? xD haha

A lot of people ask this question… So I keep it there in my notepad++ in case I need it or others need a function like this…
Cranium #5
Posted 16 January 2013 - 10:15 AM
Actually yes, it was called limitRead! And mine is just bigger because it has some extra features, like background and text colors.
remiX #6
Posted 16 January 2013 - 10:24 AM
Actually yes, it was called limitRead! And mine is just bigger because it has some extra features, like background and text colors.

Haha nice :P/>