Simulating touching screen

Hello! I have a question if is it possible to simulate touching screen?

What I mean is some function that actually will do same as you touch screen (something like touch(x, y)) and will run all listeners that are on x/y. I’ve found similar topic, but there was no satisfying answers and I hope something changed since then.

Here is the topic:

https://forums.coronalabs.com/topic/34594-way-to-simulate-a-touch-even-in-code/

If you’re touch handler is a function listener, not a table listener, simply put together a table with all the expected values that the event expects (x, y, phase, name, etc.) and call your touch handler straight up.

local fakeTouchEvent = {

   x = 100,

   y = 200,

   phase = “began”,

   name = “touch”,

   target = objectBeingTouched

   – etc.  You need to include everything listed here https://docs.coronalabs.com/api/event/touch/index.html

}

yourTouchFunction( fakeTouchEvent )

If it’s a table listener, then you would pass in the object being touched as the first parameter

yourTouchFunction( objectBeingTouched, fakeTouchEvent )

Rob

So is it the only way? There is no way to simulate touching without knowing which object is being touched, just knowing x,y?

Hi @19sheyki96,

Do you only want to know where (x,y) on the screen is being touched? No need to know any specific object? If so, just add a touch event listener to the “stage” object (Corona’s overall display stage on which all objects are placed), and detect the x and y that way.

Take care,

Brent

You can but it’s a pain.  You would have to start with the stage, take the X, Y and start traversing through the the objects on the stage and seeing if your X, Y is within the bounds of that object.  Then you could dispatch a touch event to that object.  Then keep traversing through objects.  If you see an object and it’s a group, then you would need to traverse the group as well.

Rob

That’s actually good idea that will work for me :slight_smile: Thanks

If you’re touch handler is a function listener, not a table listener, simply put together a table with all the expected values that the event expects (x, y, phase, name, etc.) and call your touch handler straight up.

local fakeTouchEvent = {

   x = 100,

   y = 200,

   phase = “began”,

   name = “touch”,

   target = objectBeingTouched

   – etc.  You need to include everything listed here https://docs.coronalabs.com/api/event/touch/index.html

}

yourTouchFunction( fakeTouchEvent )

If it’s a table listener, then you would pass in the object being touched as the first parameter

yourTouchFunction( objectBeingTouched, fakeTouchEvent )

Rob

So is it the only way? There is no way to simulate touching without knowing which object is being touched, just knowing x,y?

Hi @19sheyki96,

Do you only want to know where (x,y) on the screen is being touched? No need to know any specific object? If so, just add a touch event listener to the “stage” object (Corona’s overall display stage on which all objects are placed), and detect the x and y that way.

Take care,

Brent

You can but it’s a pain.  You would have to start with the stage, take the X, Y and start traversing through the the objects on the stage and seeing if your X, Y is within the bounds of that object.  Then you could dispatch a touch event to that object.  Then keep traversing through objects.  If you see an object and it’s a group, then you would need to traverse the group as well.

Rob

That’s actually good idea that will work for me :slight_smile: Thanks