#1
Posted 01 January 2013 - 05:59 PM
i.e: Username: 12345678910
The allowed characters would be limited to ten and no more. Can anyone help me?
#2
Posted 01 January 2013 - 06:10 PM
local function read()
local input = ''
local x,y = term.getCursorPos()
term.setCursorBlink(true)
repeat
term.setCursorPos(x,y)
term.write(input)
local ev, p1 = os.pullEvent()
if ev == 'char' then
if #input < 10 then
input = input .. p1
end
elseif ev == 'key' then
if p1 == keys.backspace then
input = input:sub(1, #input - 1)
end
end
until ev == 'key' and p1 == keys.enter
term.setCursorBlink(false)
return input
end
Or, with using the base read(), don't take any input until the user has entered a string under 10 characters (while telling the user that it needs to be under ten characters).
local input
while true do
input = read()
if #input > 10 then
print "10 character max length"
else
break
end
end
print("You typed: "..input)
#3
Posted 02 January 2013 - 01:08 AM
http://www.computerc...ngth-of-a-word/
Try using the search function.
#4
Posted 02 January 2013 - 10:30 AM
For example if I were to type SuicidalSTDz, it would stop letting me type after the tenth character.
If you know how to do this, I am all ears.
#5
Posted 02 January 2013 - 10:47 AM
#6
Posted 02 January 2013 - 10:57 AM
Orwell, on 02 January 2013 - 10:47 AM, said:
#7
Posted 02 January 2013 - 11:09 AM
local function read() local input = '' local x,y = term.getCursorPos() term.setCursorBlink(true) repeat term.setCursorPos(x,y) term.write(input) local ev, p1 = os.pullEvent() if ev == 'char' then if #input < 10 then input = input .. p1 end elseif ev == 'key' then if p1 == keys.backspace then input = input:sub(1, #input - 1) end end until ev == 'key' and p1 == keys.enter term.setCursorBlink(false) return input end
Edit:
This is one that I made a while back which I use now and then,
#8
Posted 02 January 2013 - 11:24 AM
remiX, on 02 January 2013 - 11:09 AM, said:
local function read() local input = '' local x,y = term.getCursorPos() term.setCursorBlink(true) repeat term.setCursorPos(x,y) term.write(input) local ev, p1 = os.pullEvent() if ev == 'char' then if #input < 10 then input = input .. p1 end elseif ev == 'key' then if p1 == keys.backspace then input = input:sub(1, #input - 1) end end until ev == 'key' and p1 == keys.enter term.setCursorBlink(false) return input end
#9
Posted 02 January 2013 - 11:25 AM
#10
Posted 02 January 2013 - 11:43 AM
remiX, on 02 January 2013 - 11:09 AM, said:
local function read() local input = '' local x,y = term.getCursorPos() term.setCursorBlink(true) repeat term.setCursorPos(x,y) term.write(input) local ev, p1 = os.pullEvent() if ev == 'char' then if #input < 10 then input = input .. p1 end elseif ev == 'key' then if p1 == keys.backspace then input = input:sub(1, #input - 1) end end until ev == 'key' and p1 == keys.enter term.setCursorBlink(false) return input end
Edit:
This is one that I made a while back which I use now and then,
#11
Posted 02 January 2013 - 11:58 AM
SuicidalSTDz, on 02 January 2013 - 10:57 AM, said:
Orwell, on 02 January 2013 - 10:47 AM, said:
Here is the code I linked you to. It's short and easy to understand (still quite the same as remiX's though):
local function readN(len, replaceChar)
len = len or 10
local input=""
local key = 0
term.setCursorBlink(true)
repeat
local e,p1 = os.pullEvent()
if e=="char" then
if #input < len then
input = input .. p1
term.write(replaceChar or p1)
end
elseif e=="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)
end
until p1==keys.enter
term.setCursorBlink(false)
return input
end
Edit: also, remiX's version was also posted by him in the thread I linked to, so it's not us misunderstanding your question.
#12
Posted 02 January 2013 - 03:08 PM
Orwell, on 02 January 2013 - 11:58 AM, said:
SuicidalSTDz, on 02 January 2013 - 10:57 AM, said:
Orwell, on 02 January 2013 - 10:47 AM, said:
Here is the code I linked you to. It's short and easy to understand (still quite the same as remiX's though):
local function readN(len, replaceChar)
len = len or 10
local input=""
local key = 0
term.setCursorBlink(true)
repeat
local e,p1 = os.pullEvent()
if e=="char" then
if #input < len then
input = input .. p1
term.write(replaceChar or p1)
end
elseif e=="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)
end
until p1==keys.enter
term.setCursorBlink(false)
return input
end
Edit: also, remiX's version was also posted by him in the thread I linked to, so it's not us misunderstanding your question.
#13
Posted 02 January 2013 - 03:19 PM
#14
Posted 02 January 2013 - 03:44 PM
#15
Posted 02 January 2013 - 05:40 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











