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

[lua] [question] os.pullEvent() problem

Started by ETHANATOR360, 15 July 2012 - 06:20 PM
ETHANATOR360 #1
Posted 15 July 2012 - 08:20 PM
im making a menu that requires you to press a key to open a certain thing but i cant seem to get os .pullEvent() to work, the menu prints just fine and there is no cursor until i press the key then the cursor appears and the key i pressed also apears as if os.pullEvent did not exist. code:
event, param1, param2 = os.pullEvent()
if event == "char" and param1 == "1" then clear() print ("program starting") end
MysticT #2
Posted 15 July 2012 - 08:30 PM
With just two lines of code is hard to see the problem, could you post the entire code? Or at least the part for the menu?
ETHANATOR360 #3
Posted 15 July 2012 - 09:04 PM
With just two lines of code is hard to see the problem, could you post the entire code? Or at least the part for the menu?
function clear()
term.clear()
term.setCursorPos (1,1)
end
clear()
print ("+—————————+")
print ("| OS by: ETHANATOR360 |")
print ("|—————————|")
print ("| 1.file Browser |")
print ("| 2.redmail |")
print ("| 3.my Games |")
print ("| 4.turtle controller |")
print ("| 5.lua programming shell |")
print ("| 6.web browser |")
print ("| 7.notepad |")
print ("| 8.reboot |")
print ("| 9.shutdown |")
print ("| 10.exit |")
print ("+—————————+")
event, param1, param2 = os.pullEvent()
if event == "char" and param1 == "1" then clear() print ("file browser loaded") end
(wow the menu looks really bad on the forums)
MysticT #4
Posted 15 July 2012 - 09:32 PM
Here's what you code does:
clear the screen
print the menu
wait for an event (any event)
if it was a "char" event and the parameter was "1" then clear the screen and print "file browser loaded"

I guess you want to keep waiting for an event until the user chooses one of the options, so you need a loop:

-- show the menu here
while true do
  local evt, c = os.pullEvent("char") -- wait for a "char" event
  -- now check wich key was pressed
  if c == "1" then
	print("First option selected")
	break -- stop the loop
  elseif c == "2" then
	print("Second option selected")
	break -- stop the loop
  -- add all the other options here
  end
end
ETHANATOR360 #5
Posted 15 July 2012 - 10:34 PM
thanks, and will the while loop still run if 1 is pressed?
EDIT:never mind figured it out i should of looked closer at your example code :P/>/>
MysticT #6
Posted 15 July 2012 - 10:35 PM
It will run forever. If you put the breaks where I did, it will stop after you choose a valid option.