Mouse and handling touch events

GDevelop has built-in support for handling touches and mouse inputs.

Easiest way: use "The cursor/touch is on an object" condition

The condition called “The cursor/touch is on an object” will check to see if the mouse cursor or a screen touch is made on a given object(s).

If the cursor/touch is recognized using “the cursor/touch is on an object condition”, the touched/hovered objects will be selected for the next conditions and actions.

You can use “The cursor/touch is on an object” together with “Mouse button pressed” or “Mouse button released” conditions. By default, touch will emulate a left click. This default setting allows you to detect whether the user touched a button or an object with a left click of the mouse.

Note that you can use expressions like MouseX and MouseY to get the position of the cursor in the scene:

More complex way: use conditions to check position of mouse or touch

If you want to handle the touch and cursor events separately, you can use the action “De/activate moving the mouse cursor with touches”. At this moment, touches won't simulate mouse and left clicks.

In handling touch and cursor events separately, you can use expressions to get the position of the mouse (MouseX and MouseY) as well as the position of a touch.

Multiple touches can be done at the same time. You have to store the touch identifier and pass it to expressions or conditions that will give you the position of the touch. See the example below:

See it in action! 🎮
Open this example online: https://editor.gdevelop-app.com?project=example://multitouch