Jump to content




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


  • You cannot reply to this topic
2 replies to this topic

#1 Zacherl1990

  • Members
  • 9 posts

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

#2 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

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 by Bubba, 23 November 2013 - 10:04 AM.


#3 Zacherl1990

  • Members
  • 9 posts

Posted 23 November 2013 - 10:05 AM

Thanks for your reply :) Hooking os.run() sounds promising for me. I will try that!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users