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

[Solved] Keyboard Inputs

Started by TipTricky, 18 January 2013 - 05:26 PM
TipTricky #1
Posted 18 January 2013 - 06:26 PM
I was wondering how you get keyboard inputs in a program. Like lets say i ran a program i made that needs some one to type in a password to get it to run how would i get the key board inputs.
Luanub #2
Posted 18 January 2013 - 06:35 PM
There are a couple of ways, to just capture user input try read() or io.read()

write("Enter Password: ")
local password = read("*") -- the "*" makes the text appear as * on the screen
TipTricky #3
Posted 18 January 2013 - 06:45 PM
Ok so is io.read() better to use in this case or os.pullEvent()?

io.read() seems simpler than os.pullEvent() correct?
theoriginalbit #4
Posted 18 January 2013 - 06:46 PM
it is much simpler. however it will only read ascii chars. if you wish to read other keys such as enter or control you will need to use an event loop.
TipTricky #5
Posted 18 January 2013 - 06:50 PM
Thank you for the help read() work perfectly.
dan14941 #6
Posted 18 January 2013 - 11:19 PM
you could do on an if statement:
x = read("*")
if x == "passcode" then
	    --code
3ydney #7
Posted 19 January 2013 - 12:48 AM

write("Password: ")
local input = read("*")
local pass = "123" --Password
if input == pass then
print("Welcome")
elseif input ~= pass then
print("Wrong password")
else
print("Unknown error")
end
dan14941 #8
Posted 20 January 2013 - 02:11 AM
(taken down by owner)
theoriginalbit #9
Posted 20 January 2013 - 02:19 AM

write("Password: ")
local input = read("*")
local pass = "123" --Password
if input == pass then
print("Welcome")
elseif input ~= pass then
print("Wrong password")
else
print("Unknown error")
end
This looks fine. however you don't need that second elseif doing this will do the exact same thing

if input == pass then
  -- code when they are equivalent
else
  -- code when that are not equivalent
end
Also you may want to surround the entire thing with an infinite while loop so that the program will not exit after they have pressed enter…

while true do
  -- program code here
end


Or just use my lock it has custom pass input
The "Ask a Pro" section isn't here for advertising. Its here to help people.
dan14941 #10
Posted 20 January 2013 - 12:01 PM

write("Password: ")
local input = read("*")
local pass = "123" --Password
if input == pass then
print("Welcome")
elseif input ~= pass then
print("Wrong password")
else
print("Unknown error")
end
This looks fine. however you don't need that second elseif doing this will do the exact same thing

if input == pass then
  -- code when they are equivalent
else
  -- code when that are not equivalent
end
Also you may want to surround the entire thing with an infinite while loop so that the program will not exit after they have pressed enter…

while true do
  -- program code here
end


Or just use my lock it has custom pass input
The "Ask a Pro" section isn't here for advertising. Its here to help people.
sorry