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

Timer Help

Started by mrdawgza, 29 September 2013 - 06:07 PM
mrdawgza #1
Posted 29 September 2013 - 08:07 PM
Hello, I am still working on my OS.

Now, I am busy recreating my hole OS,
so I started with startup, now
I load my custom APIs in startup, and I want to make it so that if I push S during startup, or
for the first five seconds of startup, it'll quit the startup and return to shell.

(I figured it has something to do with a timer or something)

I am working on a little code, here it is:

(I used any key just to make things easy and simple for testing)

shell.run("clear")
print("Press any key to hear a secret.")
os.startTimer(3)
if os.pullEvent("key") then
  print("ComputerCraft is awesome.")
else
os.pullEvent(timer)

end

The problem:

I want it to time down whilst listening for a key input, so then if you don't push
any key, it'll return to shell, but if you do push a key it'll show the secret.
But on that little code, it'll display the secret on key press, but it won't
return to shell as if I never pushed a key,


Am I stupid meaning it has nothing to do with timers or is there a problem?

(I will give the person who helped me most credit in my OS for helping me if the problem is resolved)
immibis #2
Posted 29 September 2013 - 08:09 PM

print("Press any key to hear a secret")
timerID = os.startTimer(3)
while true do
  event, parameter = os.pullEvent()
  if event == "timer" and parameter == timerID then
    pressedAKey = false
    break
  elseif event == "key" then
    pressedAKey = true
    break
  end
end

if pressedAKey then
  -- they pressed a key
else
  -- they didn't press a key
end
mrdawgza #3
Posted 29 September 2013 - 08:22 PM
Thank you. Topic is now probably solved.
Credited immibis.