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

Wait if there will be input in 10 seconds and then move on

Started by abouttabs, 10 July 2013 - 04:04 PM
abouttabs #1
Posted 10 July 2013 - 06:04 PM
Hi guys
I am trying to figure out if there is a way to make program wait for input (probably keystroke) for set amount of time and then move on.
I wat it as user way to shut down resume of program that is taking state from another file and would run tindefinitely if no option to reset (like this) was awailable.
I would gratly appreciate any help on this topic, either this solution or any other that would work.
Thank you for your time and effort.
Sincerely Abouttabs
Bubba #2
Posted 10 July 2013 - 06:08 PM
You can use timers to do this:



local timer = os.startTimer(10) --#The interval that you would like to wait
while true do
  local event, result = os.pullEvent()
  if event=="timer" and timer==result then --#If a timer event is pulled and its ID matches the one returned by startTimer
    break --#Break out of the loop and continue with code execution
  elseif event=="key" then
    if result == keys.enter then --#They pressed a key under the 10 second interval.
       --#Do stuff
    end
  end
end
abouttabs #3
Posted 10 July 2013 - 06:10 PM
Thank you so much sir.. I had no idea about such fancy things :-)
Bubba #4
Posted 10 July 2013 - 06:12 PM
No problem. If you encounter any issues let me know :)/>
abouttabs #5
Posted 10 July 2013 - 06:14 PM
thank you
YardKing42 #6
Posted 18 August 2013 - 12:29 AM
This is great but it only works if you press 'enter'.
I noticed if you remove line 7 it works for any key but it prints what the user presses.
Is there a way to modify the program to work for any key without leaving anything behind?
Thanks heaps, Yardking42
LordIkol #7
Posted 19 August 2013 - 02:53 PM
edit: maybe you did not remove line 7 correctly. you have to remove the end for the if statement also. else it will not work.

like this nothing is printed:



local timer = os.startTimer(10) --#The interval that you would like to wait
while true do
  local event, result = os.pullEvent()
  if event=="timer" and timer==result then --#If a timer event is pulled and its ID matches the one returned by startTimer
	break --#Break out of the loop and continue with code execution
  elseif event=="key" then
	 --print("you pressed a key")
	end
  end

if you uncomment the other line with the print it will say "you pressed a key" everytime you press a key :D/>