Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Bevy Version:0.16(outdated!)

This entire book is outdated and no longer maintained. I am keeping it online, in case anyone still finds any of the information in it useful. To adapt to newer versions of Bevy, please consult Bevy's migration guides.


Touchscreen

Relevant official examples: touch_input, touch_input_events.


Multi-touch touchscreens are supported. You can track multiple fingers on the screen, with position and pressure/force information. Bevy does not offer gesture recognition.

The Touches resource allows you to track any fingers currently on the screen:

fn touches(
    touches: Res<Touches>,
) {
    // There is a lot more information available, see the API docs.
    // This example only shows some very basic things.

    for finger in touches.iter() {
        if touches.just_pressed(finger.id()) {
            println!("A new touch with ID {} just began.", finger.id());
        }
        println!(
            "Finger {} is at position ({},{}), started from ({},{}).",
            finger.id(),
            finger.position().x,
            finger.position().y,
            finger.start_position().x,
            finger.start_position().y,
        );
    }

    for finger in touches.iter_just_released() {
        println!("Touch with ID {} just ended.", finger.id());
    }
    for finger in touches.iter_just_canceled() {
        println!("Touch with ID {} was canceled.", finger.id());
    }
}

Alternatively, you can use TouchInput events:

fn touch_events(
    mut evr_touch: EventReader<TouchInput>,
) {
    use bevy::input::touch::TouchPhase;
    for ev in evr_touch.read() {
        // in real apps you probably want to store and track touch ids somewhere
        match ev.phase {
            TouchPhase::Started => {
                println!("Touch {} started at: {:?}", ev.id, ev.position);
            }
            TouchPhase::Moved => {
                println!("Touch {} moved to: {:?}", ev.id, ev.position);
            }
            TouchPhase::Ended => {
                println!("Touch {} ended at: {:?}", ev.id, ev.position);
            }
            TouchPhase::Canceled => {
                println!("Touch {} canceled at: {:?}", ev.id, ev.position);
            }
        }
    }
}