Jump to content




[Solved] Keyboard Inputs


9 replies to this topic

#1 TipTricky

  • Members
  • 24 posts

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.

#2 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

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


#3 TipTricky

  • Members
  • 24 posts

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?

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#5 TipTricky

  • Members
  • 24 posts

Posted 18 January 2013 - 06:50 PM

Thank you for the help read() work perfectly.

#6 dan14941

  • Members
  • 45 posts

Posted 18 January 2013 - 11:19 PM

you could do on an if statement:
x = read("*")
if x == "passcode" then
	    --code


#7 3ydney

  • Members
  • 53 posts
  • LocationAdelaide, Australia.

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


#8 dan14941

  • Members
  • 45 posts

Posted 20 January 2013 - 02:11 AM

(taken down by owner)

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 02:19 AM

View PostFreePaidRemake, on 19 January 2013 - 12:48 AM, said:

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


View Postdan14941, on 20 January 2013 - 02:11 AM, said:

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.

#10 dan14941

  • Members
  • 45 posts

Posted 20 January 2013 - 12:01 PM

View PostTheOriginalBIT, on 20 January 2013 - 02:19 AM, said:

View PostFreePaidRemake, on 19 January 2013 - 12:48 AM, said:

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


View Postdan14941, on 20 January 2013 - 02:11 AM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users