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

[REQUEST] Wait for keypress or time

Started by LoganDark, 25 April 2016 - 06:34 PM
LoganDark #1
Posted 25 April 2016 - 08:34 PM
Are requests even allowed in this section?

I'd like a function that waits for a user to press a key, but after a specific amount of seconds, if the user hasn't pressed a key (it should skip instantly if you do) it skips automatically. Is that possible?

I have good knowledge with CC Lua, so feel free to explain it as technical as you want.
Edited on 25 April 2016 - 06:35 PM
masterdisasterHD #2
Posted 25 April 2016 - 08:37 PM
try this:


local timeout = 3

function ontimer()
sleep(timeout)
end

function onkey()
os.pullEvent('key')
end

parallel.waitForAny(ontimer, onkey) --This waits for either of the 2 functions to finish before continuing
--Further processing below this line
Edited on 25 April 2016 - 06:39 PM
LoganDark #3
Posted 25 April 2016 - 08:40 PM
try this:


local timeout = 3

function ontimer()
sleep(timeout)
end

function onkey()
os.pullEvent('key')
end

parallel.waitForAny(ontimer, onkey) --This waits for either of the 2 functions to finish before continuing
--Further processing below this line

Thanks!
LDDestroier #4
Posted 25 April 2016 - 08:43 PM
I actually made the help screen of my QuickDraw game so that it slowly prints the help text, but if you press a button, it instantly displays the text. It's neato
LoganDark #5
Posted 25 April 2016 - 08:45 PM
I actually made the help screen of my QuickDraw game so that it slowly prints the help text, but if you press a button, it instantly displays the text. It's neato

I just had to use parallel.waitForAny.
Lyqyd #6
Posted 25 April 2016 - 10:25 PM
Moved to Ask a Pro.
LoganDark #7
Posted 25 April 2016 - 11:31 PM
Moved to Ask a Pro.

Better to lock too, since my problem was solved. It also introduced me to parallel.waitForAny.
Lupus590 #8
Posted 26 April 2016 - 09:37 AM
Ask a pro is usually not locked as it allows others with a similar problem to post additional questions
Edited on 26 April 2016 - 10:33 AM
Bomb Bloke #9
Posted 26 April 2016 - 09:47 AM
Other users are usually encouraged to make their own threads, but it's indeed recommended for posters to keep all their questions about a given project in the one place.

In any case, there's seldom good reason to actually lock a thread. It's a fairly heavy-handed way of saying "drop this subject".