# Get touch that started/ended with touchStated() / touchEnded()

**URL:** https://discourse.processing.org/t/get-touch-that-started-ended-with-touchstated-touchended/39866
**Category:** Processing for Android
**Created:** [November 25, 2022, 10:23am UTC](https://discourse.processing.org/t/get-touch-that-started-ended-with-touchstated-touchended/39866 "2022-11-25T10:23:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![JoLi](https://avatars.discourse-cdn.com/v4/letter/j/e495f1/32.png) [@JoLi](https://discourse.processing.org/u/JoLi)
#### Post date: [November 25, 2022, 10:23am UTC](https://discourse.processing.org/t/get-touch-that-started-ended-with-touchstated-touchended/39866/1 "2022-11-25T10:23:29Z")

</div>

Hi,  
can I somehow get the touch pointer that was added/removed in the `touchStarted()` or `touchEnded()` functions?

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [November 25, 2022, 8:10pm UTC](https://discourse.processing.org/t/get-touch-that-started-ended-with-touchstated-touchended/39866/2 "2022-11-25T20:10:31Z")

</div>

Hi @JoLi,

Hmmm! It is a bit weird…

You can also uses the touchStarted/Ended with TouchEvent parameter, but the way it is implemented don’t give you the nearest expectation (actually the event which one has started/ended). Therefore it seems you need to implement the handling on your own.

Here is a quick hack which do so … (quickly tinkered on my mobile, which I really hate to do 😬 🙂, so weakly tested but should work and could be used as a starting point…)

```auto
ArrayList<Integer> touchMap;
color[] cols;

void setup() {
  fullScreen();
  touchMap = new ArrayList<Integer>();
  cols = new color[5];
  colorMode(HSB, 5, 1, 1);
  for (int i=0; i<cols.length; i++)
    cols[i] = color(i, 1, 1);
}

void touchStarted() {
  for (TouchEvent.Pointer p : touches) {
    if (!touchMap.contains(p.id)) {
      touchMap.add(p.id);
      println("touch started: " + p.id);
    }
  }
}

void touchEnded() {
  for (int i=touchMap.size()-1; i>=0; i--) {
    boolean found=false;
    for (TouchEvent.Pointer p : touches) {
      if (touchMap.get(i)==p.id) {
        found=true;
        break;
      }
    }
    if (!found) {
      println("touch ended: " + touchMap.get(i));
      touchMap.remove(i);
    }
  }
}

void draw() {
  background(0);
  noStroke();
  for (TouchEvent.Pointer p : touches) {
    fill(cols[p.id%5]);
    ellipse(p.x, p.y, 250, 250);
  }
}

```

Cheers  
— mnse

PS: Apropos… If you only need one touch, not multi touch, you can simply use mouseX/mouseY for starting and ending position… 🤓

PPS: if your Processing for Android using java8+ the checks inside the touchStarted/Ended could be written more elegant with the new features 😄

---

<div class="post-metadata">

### Author: ![JoLi](https://avatars.discourse-cdn.com/v4/letter/j/e495f1/32.png) [@JoLi](https://discourse.processing.org/u/JoLi)
#### Post date: [November 26, 2022, 5:05pm UTC](https://discourse.processing.org/t/get-touch-that-started-ended-with-touchstated-touchended/39866/3 "2022-11-26T17:05:48Z")

</div>

oh tysm!  
this is exactly what I needed!  
My project is basically a touchpad:  
the app on the phone detects 1 touched finger: action 1 is executed from a companion program on a pc.  
the app detects a second finger: action 2 is also executed  
finger 1 is not touching anymore: action 1 is stopped, action 2 is still executed  
finger 2 is also not touching anymore: action 2 is also stopped.

if it’s done, i’ll maybe post another reply to this thread with the repo(s) for the code
