lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 5-Dec-06, at 6:37 PM, Shmuel Zeigerman wrote:

Don't know if it's better but here's a way that seem
to combine the pro's of the 2 above 2 ways.

-- start of Lua example
t_events = {
  KeyPressed  = OnKeyPressed,
  LeftClick   = OnLeftClick,
  DoubleClick = function (x,y) print (x,y) end,
  -- etc.
}

function OnEvent (event, ...)
  local f = t_events [event]
  if f then return f (...) end
end

SetEvent (OnEvent)
-- end of Lua example

C provides SetEvent function, and Lua registers only
*one* event handler. C calls that handler passing
event name (a string) and any number of parameters.

Or you could just register the handler table, which would also work nicely with the Lua 5.1 module system.