Skip to content
Oleg Logvinov edited this page Aug 31, 2021 · 7 revisions

This library provides an event system for registering specific event handlers. For example, it's used to automatically mount added filesystem components or to run some tasks regularly with specified interval. There's also important interrupting feature for instant killing not responding scripts.

Contents
event.pull
event.sleep
event.addHandler
event.removeHandler

event.interruptingEnabled

Boolean variable that allows to set state of interrupting programs by presssing ctrl + alt + c keystroke

event.pull(...): ...

Works the same way as computer.pullSignal(...) do, but also calls registered event handlers if needed and checks interrupting status:

while true do
  local e1, e2, e4, e4 = event.pull()
  if e1 == "touch" then
    -- Do something when touch event was received
  end
end

event.sleep(float delay)

Sleeps delay seconds via busy-wait concept. This method allows event handlers to be processed if any event occurs during sleeping.

event.sleep(0.5)

event.addHandler(function func[, int interval, int times]): table handler

Registers an event handler wrapper for given function and returns it.

Every registered handler will be analyzed for the need to run during each event.pull() call. When handler is being run, it receives values returned from event.pull() as arguments.

You can specify an interval in seconds between each run of given handler. By default it's set to nil, i.e. handler runs every event.pull() call without any delay.

You can also specify number of times that given handler will be run before being removed automatically. By default it's set to infinity.

event.addHandler(function(e1, e2, e3, e4)
  if e1 == "key_down" and e4 == 28 then
    -- Do someting every Return key press
  end
end)

event.addHandler(function()
  -- Do someting every 5 seconds
end, 5)

event.addHandler(function()
  -- Do someting every 5 seconds 10 times
end, 5, 10)

while true do
  event.pull()
end

event.removeHandler(table handler): boolean success

Tries to unregister created event handler. Returns true if it was registered and false otherwise.

local handler = event.addHandler(function()
  -- Do something
end)

event.removeHandler(handler)