Mouse and handling touch events

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

Handle clicks and taps on objects

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). This condition uses the rectangular dimensions of the object and does not consider any collision masks.

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:

Handle manually multi-touch gestures (advanced)

When only one touch is done at the same times, the mouse events can be used to keep things simple. Indeed, by default touch events are mirrored by mouse events. This can be disabled with the action “De/activate moving the mouse cursor with touches” when handling both multi-touch and mouse events to avoid echo.

Multiple touches can happen at the same time. The touch identifier must be used to tell them apart. It can be done by following these steps:

  • At the start of a touch, the touch identifier can be get with the StartedTouchId(Number) expression.

  • When the touch moves, the touch identifier is needed to make sure to follow the right touch. A variable can be used to ensure this.

In GDevelop 5.0.131 and previous releases, the touch handling was a bit different and looked like this:

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