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

Check For Key Press Without Pausing?

Started by Roboguy99, 18 November 2013 - 10:56 AM
Roboguy99 #1
Posted 18 November 2013 - 11:56 AM
I have been working on a missile computer system with ICBM and the built in CC API for it. When you launch a missile, it does a 10 second countdown. During this countdown, I want to check for key presses of any kind and run another piece of code which asks you to enter a password to confirm the cancelling of the launch. The part I need help with, is how do I check for a key press but not wait for one? All my code so far looks like this:

Spoiler

local i = 0
local j = 10
local password = "hg548"

launcher = peripheral.wrap("left")

function checkForKeyPress()
while true do
local event, cancel = os.pullEvent("key")

if event == "key" then
write("Enter password to continue: ")
input = read("*")

if input == password then
print("Launch aborted. The computer will now restart.")
sleep(1)
term.clear()
term.setCursorPos(1,1)
os.reboot()
end
end
end
end

write("Confirm launch: (y/n): ")
ans = read()

if ans == "y" then
  write("Enter password to continue: ")
  input = read("*")
  if input == password then
print("Launch confirmed.")
sleep(1)
term.clear()
term.setCursorPos(1,1)

for i=1, 100 do
print("Security checking..." .. i .. "%")
sleep(0.05)
term.clear()
term.setCursorPos(1,1)
end

while j~=0 do
print("Launching in " .. j .. " seconds")
sleep(1)
term.clear()
term.setCursorPos(1,1)
--parallel.run(checkForKeyPress)
j = j-1
end

launcher.launch()
os.reboot()
else
print("Password incorrect. The computer will now restart")
sleep(1)
term.clear()
term.setCursorPos(1,1)
os.reboot()
end
end

if ans == "n" then
print("Launch cancelled. The computer will now restart")
sleep(1)
term.clear()
term.setCursorPos(1,1)
os.reboot()
end
Lyqyd #2
Posted 18 November 2013 - 02:12 PM
Don't sleep. Listen for timer and key events and start timers as necessary instead.
Roboguy99 #3
Posted 20 November 2013 - 07:54 AM
And how would I go about that? I looked on the wiki, but it wasn't much help. Sorry, I'm a bit of a Lua noob at the moment…
apemanzilla #4
Posted 20 November 2013 - 09:45 AM
Here's a snippet I wrote on the fly (on my iPad, might not be formatted correctly)

--Ten second countdown with canceling by pressing "C"
local t = 10 --Change to amount of time if you need to.
local cancel = "c" --Change to cancel key
os.startTimer(1)
while true do
  local event, a = os.pullEvent()
  if event == "timer" then
    t = t - 1
    if t == 0 then break else os.startTimer(1) end
  elseif event == "char" and a == cancel then
    break
  end
end
if t == 0 then
  --Do stuff
else
  --Launch cancelled
end
Haven't tested this but it should work fine.
To have a cancel password, look into the parallels API. (It's built into CC)

Edit: Fixed the elseif statement
Edit 2: Please indent your code :(/>
Edited on 20 November 2013 - 08:55 AM
Roboguy99 #5
Posted 20 November 2013 - 10:16 AM
Edit 2: Please indent your code :(/>

Sorry, it seemed to unformat itself when I uploaded it. Testing the code out now.
Bomb Bloke #6
Posted 20 November 2013 - 04:35 PM
Bear in mind that timers can potentially be started by other sources. When you start one, it's best to record which timer it is, so you can be sure that's the one expiring.

Eg,

local myTimer = os.startTimer(1)

if event == "timer" and a == myTimer then

if t == 0 then break else myTimer = os.startTimer(1) end
apemanzilla #7
Posted 21 November 2013 - 08:55 AM
Bear in mind that timers can potentially be started by other sources. When you start one, it's best to record which timer it is, so you can be sure that's the one expiring.

Eg,

local myTimer = os.startTimer(1)

if event == "timer" and a == myTimer then

if t == 0 then break else myTimer = os.startTimer(1) end
Didn't know they returned anything on start :P/>