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

How do I run a loop until a key is pressed?

Started by alexdr5398, 19 August 2014 - 03:42 PM
alexdr5398 #1
Posted 19 August 2014 - 05:42 PM
I have tried using os.pullEvent("key") but that will just wait until a key is pressed, then run the loop once.

Here is what I have written:


local ev = os.pullEvent("key")
repeat
  nameCheck()
  print("Searching...")
until ev == "key"

How do I make the program quit as soon as a key is pressed, but still run normally until then?

EDIT: The rest of the code.


os.loadAPI("ocs/apis/sensor")

local prox = sensor.wrap("top")
local name = ""

function nameCheck()
local targets = prox.getTargets()
  for k,v in pairs(targets) do
    if ((v.Name) == "Player") then
	  details = prox.getTargetDetails(k)
	  if not(details == nil) then
	    player_name = details.Username
	    print(player_name.." has entered the radius")
	  end
    end
  end
  return player_name
end
local ev = os.pullEvent("key")
repeat
  nameCheck()
  print("Searching")
until ev == "key"
Edited on 19 August 2014 - 05:03 PM
Lyqyd #2
Posted 19 August 2014 - 05:45 PM
You'd need to have an event handling loop. You'd start a timer event before the loop. In the loop, if the event was a timer event, you'd tick the work you are doing and start a new timer. If it was a key event, you'd exit the program.

Please post the rest of your code.
alexdr5398 #3
Posted 19 August 2014 - 07:03 PM
You'd need to have an event handling loop. You'd start a timer event before the loop. In the loop, if the event was a timer event, you'd tick the work you are doing and start a new timer. If it was a key event, you'd exit the program.

Please post the rest of your code.

How do I create an event handling loop in lua? I added the rest of the code btw.
Dragon53535 #4
Posted 19 August 2014 - 08:54 PM
If you're wanting to run one piece of code until another happens then you could try using the parallel API and use parallel.waitForAny() in which case it would run through both functions until something happens.
You could write something like

local timer
function timerstart()
  if not timer then
	timer = os.startTimer(10)
  end
end
function events()
  while true do
	local event, param1 = os.pullEvent()
	if event == "timer" and param1 == timer then
	  print("The timer has ended")
	  error()
	elseif event == "char" and param1 == "c" then
	  print("someone pressed c")
	  error()
	end
  end
end
while true do
parallel.waitForAny(timerstart,events)
end
which would start the timer, and after 10 seconds stop the program. OR if you wanted to stop the program before, you could easily press c and it would leave the program as well. Now using those examples you get both a key event, and a timer event all rolled up into one. However you could remove the timer and put your sensor code in and it would work all the same.



Now if you're using the sensor to make a door lock, i already made one using snirkimmington's base code (here) however i modified it so that i could easily change who could and couldn't access the room i'm using. Also his code didn't shut the door when someone left the radius he specified, it only shut the door when someone left the sensor's radius. (here)
Edited on 19 August 2014 - 07:04 PM
Bomb Bloke #5
Posted 20 August 2014 - 03:14 AM
The parallel API is a bit overkill here. It has its uses for similar purposes, but here's a version of the above which doesn't use it:

local myTimer = os.startTimer(0)

while true do
	local event, param1 = os.pullEvent()

	if event == "timer" and param1 == myTimer then
		nameCheck()
		myTimer = os.startTimer(1)
	elseif event == "char" and param1 == "c" then
		print("someone pressed c")
		error()
	end
end

You'll probably find it worthwhile to read up on os.startTimer().
RickiHN #6
Posted 21 August 2014 - 08:50 PM
Why not:

repeat
   event,p1 = os.pullEvent()
   stuff

until event=="char" and p1==("x")
Replace "x" with whatever key you want.
Or does it have to work with "any key"?
Edited on 21 August 2014 - 06:53 PM
theoriginalbit #7
Posted 22 August 2014 - 06:46 AM
Why not:

repeat
   event,p1 = os.pullEvent()
   --# stuff
until event=="char" and p1==("x")
Replace "x" with whatever key you want.
Or does it have to work with "any key"?
because that doesn't queue timers, meaning the loop will only run when an event is fired, not all the time like the OP wants.
Gumball #8
Posted 18 September 2014 - 03:05 AM
use this code:

keys = os.pullEventRaw()
running = true

while running do
–code here



–loop stopper
if(event == "key") then
running = false
end
Edited on 18 September 2014 - 01:05 AM
Dog #9
Posted 18 September 2014 - 03:20 AM
Have you tested your code there, bluebird173? As it's written it would not work the way you think it would, nor the way OP requested. Bomb Bloke's suggestion does, however, work *and* do what the OP requested. You might want to re-read the thread and double check your code there.
Bomb Bloke #10
Posted 18 September 2014 - 03:30 AM
Dragon53535's code works too, mind you. I was just condensing it. Bit of a moot point considering the OP hasn't logged in for the best part of a month.
Dog #11
Posted 18 September 2014 - 03:31 AM
Good points, both of them - thanks, Bomb Bloke :)/>