Touch Input

1. Introduction

Mobile devices (Android, iOS, Nintendo Switch) allow to use touch screen where user literally touches the screen to indicate clicking / dragging.

Note
Desktop devices actually can have a touch screen too. Steam Deck allows to use a touch screen on a platform that is technically just Linux. Though right now we don’t have any special support for it, we just listen on mouse events from OS.

2. Just handle mouse "left" button

The touches on the touch screen are reported exactly like using the left mouse button on desktops. This is exactly what you want in general to have cross-platform support for both mouse and touch.

So to write a cross-platform application that responds to both touches and mouse, just react to:

3. Handling multi-touch

If you want to additionally handle multi- touch on touch devices, you can also look at

See example in examples/mobile/drawing_toy for a demo using multi-touch. Screens below are from Android and iOS.

Drawing Toy on Android Drawing Toy on iOS

4. Detecting devices with touch screen

If you want to conditionally behave differently on devices with touch screen, you can always check ApplicationProperties.TouchDevice. But in usual cases, you shouldn’t really need it.

Note
For easy testing, the ApplicationProperties.TouchDevice property is settable, so you can set this property to true even on desktops, to test your "touch device mode" in a desktop application.
Note
Some devices with touch screen, like Android and Nintendo Switch, allow to plug external keyboard and mouse. We don’t support it now in any special way, but we will in the future, and then user will be able to use normal mouse behavior in your mobile game. It also means that inferring too much from ApplicationProperties.TouchDevice being true isn’t wise — the device may have a touch screen, but user may be using external keyboard / mouse.

5. Remember what is not possible

Remember that some things are not possible with a touch screen. Namely:

  • You will never have mouse press with button different than buttonLeft

  • You will never observe motion when no mouse button is pressed, of course. Since we can only observe dragging when user is touching the screen. This means that e.g. mouse look cannot work the same — while it could observe motion when dragging, this changes user experience and ultimately you just need different controls for 3D navigation on mobile. We provide TCastleTouchNavigation for this.


To improve this documentation just edit this page and create a pull request to cge-www repository.