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

Method To Monitor Events Without Removing Them From The Queue Or Way To Detect Program End?

Started by Zacherl1990, 23 November 2013 - 08:19 AM
Zacherl1990 #1
Posted 23 November 2013 - 09:19 AM
Hello,

im currently writing a touchscreen api and have to pull for the "monitor_touch" event. I dont want the user to redirect all of these events to my api, because it will complicate the event handling. This is why i came up with a simple hook:

local O_pullEventRaw = os.pullEventRaw

function C_pullEventRaw()
  event, a1, a2, a3, a4, a5 = O_pullEventRaw()
  print("hooked: " .. event)
  return event, a1, a2, a3, a4, a5
end
os.pullEvent = C_pullEventRaw

while true do
  event = os.pullEvent("mouse_click")
  print("normal mouse click event")
end

That works fine … until the program ends … In this case the hook will stay active until i restart the computer.

Is there any better way to monitor events without actively pulling for them (coroutines dont work)? Or is there a way to detect the end of my program (terminate event is not sufficient, because it is not fired on a normal program termination)?

Best regards
Zacherl
Bubba #2
Posted 23 November 2013 - 09:49 AM
If your program is running other programs, you can detect when the program ends simply by the fact that the os.run or dofile function has ended.


local old = os.pullEvent
os.pullEvent = newPullEvent
os.run("some_program")
os.pullEvent = old

If you need to detect whether a program has ended directly from the os.pullEvent hook, I would suggest something like this:


local oldRun = os.run
os.run = function(...)
  oldRun(unpack(arg))
  os.queueEvent("program_terminate")
end

This would need to be run in a separate program prior to everything else, except perhaps your pullEvent override. After that however, it would queue a "program_terminate" event whenever a program is exited and your hook could handle it as you wish.
Edited on 23 November 2013 - 09:04 AM
Zacherl1990 #3
Posted 23 November 2013 - 10:05 AM
Thanks for your reply :)/> Hooking os.run() sounds promising for me. I will try that!