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

Adding Key Press'?

Started by Blocky_Balboa, 04 August 2012 - 08:31 PM
Blocky_Balboa #1
Posted 04 August 2012 - 10:31 PM
Hey Pro's.
Im new to Lua coding (found out about it last night) and am trying to write a basic code to open a door with Key Press'.
So far i've gotten it to say [Press [O] to open] or [Press [C] to Close]
But when I press the keys nothing happens.
Could you take a look and tell me how its wrong or any ways to improve it?
thanks :P/>/>

My code so far:

print("What do you want do do?")
print("[Press [O] to Open] or [Press [C] to Close]")
while true do
event, pl = os.pullEvent(38)
end
    if pl == 24 then
    rs.setOutput(left,true)
    print("Door Opened")
    sleep(1)
end
    if pl == 46 then
    rs.setOutput(left,false)
    print("Door Closed")
    sleep(1)
end
MysticT #2
Posted 04 August 2012 - 10:44 PM

print("What do you want do do?")
print("[Press [O] to Open] or [Press [C] to Close]")
while true do
event, pl = os.pullEvent(38) -- os.pullEvent takes an optional string parameter, not a number
end -- this end shouldn't be here
	if pl == 24 then
	rs.setOutput(left,true) -- left should be a string, so you need quotes: "left"
	print("Door Opened")
	sleep(1)
end
	if pl == 46 then
	rs.setOutput(left,false) -- same as above
	print("Door Closed")
	sleep(1)
end

Fixed code (I used "char" event instead of "key", it's easier)

print("What do you want to do?")
print("[Press [O] to Open] or [Press [C] to Close]")
while true do
  local evt, c = os.pullEvent("char") -- wait for a key press
  c = string.lower(c) -- convert to lower case, so you can press o or O
  if c == "o" then
	rs.setOutput("left", true)
	print("Door Opened")
	sleep(1)
  elseif c == "c" then
	rs.setOutput("left", false)
	print("Door Closed")
	sleep(1)
  end
end
Blocky_Balboa #3
Posted 05 August 2012 - 03:01 AM
Thanks so much :P/>/>