Send An Input Event From Lua
In some cases you may wish to be able to send input/pointer events (gre.press, gre.release, etc.) to your application from Lua.
You can do this simply by constructing the desired event with an appropriate data payload and then just sending that event to your UI.
If we take a look at the documentation for gre.send_event we can see that it can take a table as an argument containing the following:
- name A string containing the event to send
- target A string containing the object to target the event to (see Storyboard IO) (optional)
- format A string format of the event data payload (optional)
- data A table whose keys match up with the keys specified in the format (optional)
So now let’s take a look at how a gre.press event is constructed. The documentation shows the list of fields that come with gre.press. This is the same for every kind of pointer/input event.
Let’s say we wanted to press a button with a fully qualified name of “Layer.Button” for simplicity’s sake.
We can reconstruct this event in Lua like:
local event = {}
event.name = "gre.press"
event.target = "Layer.Button"
event.format = "4u1 button 4u1 timestamp 2u1 subtype 2s1 x 2s1 y 2s1 z 2s1 id 2s1 spare"
event.data = {button=1, timestamp=0, subtype=0, x=gre.get_value('Layer.Button.grd_x'), y=gre.get_value('Layer.Button.grd_y'), z=0, id=0, spare=0}
gre.send_event(event)
As you can see, the x and y position of the button are being used as positional coordinates for the press event so that the gre.press occurs within the button’s bounds.
This is all that is needed to be able to “press” a button from Lua. The same can be done for the other pointer events as well.
Comments
Please sign in to leave a comment.