local function getArraySize(array)
local count = 0
for _ in ipairs(array) do
count=count+1
end
return count
end
local function numbersOnly(pre)
local x,y=term.getCursorPos()
num={}
while true do
if pre then write(pre) end
for _,v in ipairs(num) do
write(v)
end
--write(' <')
_,key=os.pullEvent('key')
if key == 2 or key == 79 then
table.insert(num,1)
elseif key == 3 or key == 80 then
table.insert(num,2)
elseif key == 4 or key == 81 then
table.insert(num,3)
elseif key == 5 or key == 75 then
table.insert(num,4)
elseif key == 6 or key == 76 then
table.insert(num,5)
elseif key == 7 or key == 77 then
table.insert(num,6)
elseif key == 8 or key == 71 then
table.insert(num,7)
elseif key == 9 or key == 72 then
table.insert(num,8)
elseif key == 10 or key == 9 then
table.insert(num,9)
elseif key == 11 or key == 82 then
table.insert(num,0)
elseif key == 14 then
table.remove(num, getArraySize(num))
elseif key == 28 then
final=0
size=getArraySize(num)
for i,v in ipairs(num) do
final=final+(v*10^(size-i))
end
print()
return final
end
term.setCursorPos(x,y)
term.clearLine()
end
end
I can add a stationary cursor, but since I don't know how to time out an event, I can't make it blink. Any suggestions?
Add blinking cursor to a numbers only input function
#1
Posted 04 February 2014 - 04:16 PM
#2
Posted 04 February 2014 - 04:25 PM
Sidenote to myself: don't put that many links in one post.
Edit: Just had a read through your code and I have some suggestions and tips:
You check for specific keys, if they were pressed. That's not very good IMO (In My Opinion). I suggest using 'char' event. 'char' event returns the actual writable character that was pressed, so you don't have to check for specific key presses.
local event, char = os.pullEvent("char")
print("You pressed '" .. char .. "' character.") --// If you would press 'e' -->> You pressed 'e' character.
Also note that it will always return a string, so if you want that string to become a number just use tonumber(StringThatIsANumber) function:
local text = "123" --// We want this text to become a number print(type(text)) -->> string local number = tonumber(text) --// Convert that text to a number print(type(number)) -->> number --// Note: function type() returns the type of the variable, ex.: string, number, table, etc.
But what if the user pressed 'e' or any other non-number character/key? If you give a non-number value to tonumber it will return nil (nothing):
local notANumber = "test string"
if tonumber(notANumber) ~= nil then --// If tonumber() didn't return nil
print("It's a valid number!")
else
print("It's not a number!")
end
--// Note: You can use this instead:
if tonumber(notANumber) then
--// Because every value non-nil or non-false value evaluates to true
Another tip is about getting table's length. You can use # in front of your table's variable's name to get it's length:
local myTable = {1, 2, 3}
print(#myTable) -->> 3
And finally, looks like you know how to use local variables, then why didn't you localized your event and it's returnings, and the num table and a few other variables?
num = {}
...
_,key=os.pullEvent('key')
...
final=0
size=getArraySize(num)
Edited by MKlegoman357, 04 February 2014 - 04:56 PM.
#3
Posted 04 February 2014 - 11:22 PM
MKlegoman357, on 04 February 2014 - 04:25 PM, said:
Sidenote to myself: don't put that many links in one post.
Edit: Just had a read through your code and I have some suggestions and tips:
You check for specific keys, if they were pressed. That's not very good IMO (In My Opinion). I suggest using 'char' event. 'char' event returns the actual writable character that was pressed, so you don't have to check for specific key presses.
local event, char = os.pullEvent("char")
print("You pressed '" .. char .. "' character.") --// If you would press 'e' -->> You pressed 'e' character.
I'm still quite perplexed on adding the cursor though.
Also note that it will always return a string, so if you want that string to become a number just use tonumber(StringThatIsANumber) function:
local text = "123" --// We want this text to become a number print(type(text)) -->> string local number = tonumber(text) --// Convert that text to a number print(type(number)) -->> number --// Note: function type() returns the type of the variable, ex.: string, number, table, etc.
But what if the user pressed 'e' or any other non-number character/key? If you give a non-number value to tonumber it will return nil (nothing):
local notANumber = "test string"
if tonumber(notANumber) ~= nil then --// If tonumber() didn't return nil
print("It's a valid number!")
else
print("It's not a number!")
end
--// Note: You can use this instead:
if tonumber(notANumber) then
--// Because every value non-nil or non-false value evaluates to true
Another tip is about getting table's length. You can use # in front of your table's variable's name to get it's length:
local myTable = {1, 2, 3}
print(#myTable) -->> 3
And finally, looks like you know how to use local variables, then why didn't you localized your event and it's returnings, and the num table and a few other variables?
num = {}
...
_,key=os.pullEvent('key')
...
final=0
size=getArraySize(num)
For the local statements, some of them don't have them because this was my first version of the code, and I wasn't really too worried about them the moment. Also, I'm not just trying to get only a number, but I was setting it so the only keys that could be pressed were the numbers.
I didn't know about the char event and getting the length of a table the easy way, will definitely implement it as soon as I get around to modifying the code.
Since the when I want the cursor is actually during os.pullEvent() and not read(), it doesn't show up and that's why I'm looking for a way to add it.
Thanks for the tips!
Edit:
Thanks to your comments, I have dramatically decreased the size and complexity of the function:
local function numbersOnly(pre)
local x,y=term.getCursorPos()
local num={}
while true do
if pre then write(pre) end
for _,v in ipairs(num) do
write(v)
end
local event,key=os.pullEvent()
if event=='char' then
if tonumber(key) then
key=tonumber(key)
if key>=0 and key<10 then
table.insert(num,tonumber(key))
end
end
elseif event=='key' then
if key==14 then
table.remove(num, #num)
elseif key==28 then
final=0
size=#num
for i,v in ipairs(num) do
final=final+(v*10^(size-i))
end
print()
return final
end
end
term.setCursorPos(x,y)
term.clearLine()
end
end
Edited by Himself12794, 05 February 2014 - 12:20 AM.
#4
Posted 04 February 2014 - 11:30 PM
if you want people to type only numbers you could also check the key code returned from the key event and make sure that it is between keys.one (code 2) and keys.zero (code 11). Additionally depending on requirements also allowing keys.period (code 52) and keys.minus (code 12). Key codes.
#5
Posted 05 February 2014 - 12:23 AM
theoriginalbit, on 04 February 2014 - 11:30 PM, said:
if you want people to type only numbers you could also check the key code returned from the key event and make sure that it is between keys.one (code 2) and keys.zero (code 11). Additionally depending on requirements also allowing keys.period (code 52) and keys.minus (code 12). Key codes.
This is why I like to get feedback on my work from the forums. I always find there is a simpler way, and I learn much.
I guess the real question I'm asking is, How do I do a timeout event on os.pullEvent()?
Edited by Himself12794, 05 February 2014 - 12:22 AM.
#7
Posted 05 February 2014 - 12:33 AM
Edited by Himself12794, 05 February 2014 - 12:34 AM.
#9
Posted 05 February 2014 - 12:50 AM
local function numbersOnly(pre)
local cursor = true
local x,y=term.getCursorPos()
local num={}
while true do
if pre then write(pre) end
for _,v in ipairs(num) do
write(v)
end
if cursor then write('_') end--else write(' ') end
local myTimer = os.startTimer(0.25)
local event,key=os.pullEvent()
if event=='char' then
if tonumber(key) then
key=tonumber(key)
if key>=0 and key<10 then
table.insert(num,key)
end
end
elseif event=='timer' then
cursor = not cursor
elseif event=='key' then
if key==14 then
num[#num]=nil
elseif key==28 then
final=0
size=#num
for i,v in ipairs(num) do
final=final+(v*10^(size-i))
end
term.setCursorPos(x,y)
term.clearLine()
if pre then write(pre) end
for _,v in ipairs(num) do
write(v)
end
print()
return final
end
end
term.setCursorPos(x,y)
term.clearLine()
end
end
theoriginalbit, on 05 February 2014 - 12:38 AM, said:
Edited by Himself12794, 05 February 2014 - 12:50 AM.
#10
Posted 05 February 2014 - 12:53 AM
honestly though using a table to make your number is definitely inefficient!
#11
Posted 05 February 2014 - 01:01 AM
#12
Posted 05 February 2014 - 01:02 AM
theoriginalbit, on 05 February 2014 - 12:53 AM, said:
honestly though using a table to make your number is definitely inefficient!
#13
Posted 05 February 2014 - 01:06 AM
Himself12794, on 05 February 2014 - 01:02 AM, said:
#14
Posted 05 February 2014 - 01:06 AM
Lyqyd, on 05 February 2014 - 01:01 AM, said:
Again, I am sorry for being so dense.
Problem solved!
#15
Posted 05 February 2014 - 01:20 AM
theoriginalbit, on 05 February 2014 - 01:06 AM, said:
Himself12794, on 05 February 2014 - 01:02 AM, said:
local function numbersOnly(pre)
term.setCursorBlink(true)
local x,y=term.getCursorPos()
local num=''
while true do
if pre then write(pre) end
write(num)
local event,key=os.pullEvent()
if event=='char' then
if tonumber(key) then
if tonumber(key)>=0 and tonumber(key)<10 then
num=num..key
end
end
elseif event=='key' then
if key==14 then
num[#num]=nil
elseif key==28 then
return tonumber(num)
end
end
term.setCursorPos(x,y)
term.clearLine()
end
end
#16
Posted 05 February 2014 - 01:21 AM
Himself12794, on 05 February 2014 - 01:20 AM, said:
Edited by theoriginalbit, 05 February 2014 - 01:23 AM.
#17
Posted 05 February 2014 - 01:29 AM
theoriginalbit, on 05 February 2014 - 01:21 AM, said:
Himself12794, on 05 February 2014 - 01:20 AM, said:
#18
Posted 05 February 2014 - 01:30 AM
Himself12794, on 05 February 2014 - 01:29 AM, said:
#19
Posted 05 February 2014 - 01:45 AM
theoriginalbit, on 05 February 2014 - 01:30 AM, said:
Himself12794, on 05 February 2014 - 01:29 AM, said:
local function numbersOnly(pre)
term.setCursorBlink(true)
local x,y=term.getCursorPos()
local num=''
while true do
if pre then write(pre) end
write(num)
local event,key=os.pullEvent()
if event=='char' and tonumber(key) then
num=num..key
elseif event=='key' then
if key==14 then
num=string.sub(num, 1, -2)
elseif key==28 then
print()
term.setCursorBlink(false)
return tonumber(num)
end
end
term.setCursorPos(x,y)
term.clearLine()
end
end
Thanks for all the help!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











