# TouchScreens and mousereleased()

**URL:** <https://discourse.processing.org/t/touchscreens-and-mousereleased/6135>\
**Category:** Processing for Android\
**Created:** [December 1, 2018, 4:53am UTC](https://discourse.processing.org/t/touchscreens-and-mousereleased/6135 "2018-12-01T04:53:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![metype](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/metype/32/7040_2.png) [@metype](https://discourse.processing.org/u/metype)\
**Post date:** [December 1, 2018, 4:53am UTC](https://discourse.processing.org/t/touchscreens-and-mousereleased/6135/1 "2018-12-01T04:53:20Z")

</div>

When using a mouse and keyboard this code works fine and draws like it is supposed to, but when using a touchscreen and taking my finger off of the screen, the mouseReleased() function never seems to get called.

```auto
color c=color(252,41,41);
int x, y, xp, yp;
int strokeW=1,flag=0;
void setup()
{
  //size(1280, 720);
  fullScreen();
  background(0);
}
void draw()
{
  noStroke();
  fill(c);
  rect(0, 0, 30, 30);
  stroke(c);
  strokeWeight(strokeW);
  if(flag==1){
    line(mouseX, mouseY, pmouseX, pmouseY);
  }
}
 
void mouseDragged() {
  flag=1;
}
void mouseReleased(){
  flag=0;
}
 
 
void keyPressed() {
  if (key == 'c')  
    background(0);
    flag = 0;
  if (key == CODED) {
    if (keyCode == UP) strokeW++; 
    if (keyCode == DOWN) strokeW--;
  }
  if (strokeW<0)strokeW=1;
  if(key== ' ') c = color(random(0,255),random(0,255),random(0,255));
}

```

I don’t know if this is Processing or my touchscreen, but I need help resolving this issue.

---

<div class="post-metadata">

**Author:** ![metype](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/metype/32/7040_2.png) [@metype](https://discourse.processing.org/u/metype)\
**Post date:** [December 1, 2018, 8:20am UTC](https://discourse.processing.org/t/touchscreens-and-mousereleased/6135/2 "2018-12-01T08:20:27Z")

</div>

I mostly fixed the problem by setting the flag to 0 if the mouse’s position 6 frames ago is the same as its position on this frame, as I intend to only use this personally, it shouldn’t be a big deal.

```auto
color c=color(252,41,41);
int x, y, xp, yp;
int strokeW=1,flag=0;
int px;
int pmx2,pmx3,pmx4,pmx5,pmx6;
void setup()
{
  //size(1280, 720);
  fullScreen();
  background(0);
}
void draw() {
  if(pmx6 == pmouseX) {
    flag=0;
  }
  pmx6 = pmx5;
  pmx5 = pmx4;
  pmx4 = pmx3;
  pmx3 = pmx2;
pmx2 = pmouseX;
  noStroke();
  fill(c);
  rect(0, 0, 30, 30);
  stroke(c);
  strokeWeight(strokeW);
  if(flag==1 && pmouseX > 0){
    line(mouseX, mouseY, pmouseX, pmouseY);
  }
}
 
void mouseDragged() {
  flag=1;
}
void mouseReleased(){
  flag=0;
}
 
 
void keyPressed() {
  if (key == 'c')  
    background(0);
    flag = 0;
  if (key == CODED) {
    if (keyCode == UP) strokeW++; 
    if (keyCode == DOWN) strokeW--;
  }
  if (strokeW<0)strokeW=1;
  if(key== ' ') c = color(random(0,255),random(0,255),random(0,255));
}

```

---

<div class="post-metadata">

**Author:** ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)\
**Post date:** [December 1, 2018, 9:40am UTC](https://discourse.processing.org/t/touchscreens-and-mousereleased/6135/3 "2018-12-01T09:40:05Z")

</div>

> [@mouseClicked() not working](https://discourse.processing.org/t/mouseclicked-not-working/4004):
>
> Why mouseClicked() not working on APDE? Im using APDE 0.5.0 Code: [https://pastebin.com/91wyHgzY](https://pastebin.com/91wyHgzY)

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [September 21, 2019, 4:38am UTC](https://discourse.processing.org/t/touchscreens-and-mousereleased/6135/5 "2019-09-21T04:38:12Z")

</div>

Hello! I found some code and edited it to get it to detect multiple finger taps and the location! I hope it helps!

```auto
import android.view.MotionEvent;

 float x,y;

String[] fontList;

PFont androidFont;

int circleBaseSize = 512; // change this to make the touche circles bigger\smaller.

// above this line i didnt change anything

// i added boolean tap which can only be true 
// or false. down the page it is true when 
// any amount of fingers touch the screen. if
// no fingers touch it then its false
boolean tap;

// this will be true when i remove my finger
// but then be false as its only run 1 time
boolean fingerRemoved;

// these will tell you which finger you have
// down
boolean finger1 = false;
boolean finger2 = false;
boolean finger3 = false;
boolean finger4 = false;
boolean finger5 = false;

// these are the location of the current finger
float mouseX1 = -999999;
float mouseY1 = -999999;
float mouseX2 = -999999;
float mouseY2 = -999999;
float mouseX3 = -999999;
float mouseY3 = -999999;
float mouseX4 = -999999;
float mouseY4 = -999999;
float mouseX5 = -999999;
float mouseY5 = -999999;

// this tells you how many fingers are down
int fingers = 0;

// i changed nothing in setup
void setup() {

  size(displayWidth, displayHeight);

  // Fix the orientation so the sketch won't reset when rotated.

  orientation(PORTRAIT);

  stroke(255);

  smooth();

  // Setup Fonts:

  fontList = PFont.list();

  androidFont = createFont(fontList[0], 16, true);

  textFont(androidFont);

  textAlign(LEFT);

}

 

void draw() {
background(0);
// DO NOT REMOVE fingerRun();!!!!!
// without it nothing works
fingerRun();

// this says if finger 1 is down then...
if(finger1){
// colors it
fill(color(255,0,0));
//draws circle
ellipse(mouseX1,mouseY1,200,200);
// color
fill(255);

// THIS IS IMPORTANT!! mouseX1 and mouseY1 are
// the location of finger 1
text("finger 1",mouseX1 - 50,mouseY1 - 70);
}

// what i said applies here too like mouseX2 
// and mouseY2 are the location of finger 2
// and so on
if(finger2){
fill(color(0,255,0));
ellipse(mouseX2,mouseY2,200,200);
fill(255);
text("finger 2",mouseX2 - 50,mouseY2 - 70);
}

if(finger3){
fill(color(0,0,255));
ellipse(mouseX3,mouseY3,200,200);
fill(255);
text("finger 3",mouseX3 - 50,mouseY3 - 70);
}

if(finger4){
fill(color(255,255,0));
ellipse(mouseX4,mouseY4,200,200);
fill(255);
text("finger 4",mouseX4 - 50,mouseY4 - 70);
}

if(finger5){
fill(color(255,0,255));
ellipse(mouseX5,mouseY5,200,200);
fill(255);
text("finger 5",mouseX5 - 50,mouseY5 - 70);
}

}

 

void infoCircle(float x, float y, float siz, int id) {

  // What is drawn on sceen when touched.

  float diameter = circleBaseSize * siz;

  

  

  

 

// i removed the circle

// this says if any amount of fingers are 
// down then...
if(tap){
// this says if finger 1 is down...
if(id == 0){
// then finger 1 = true
finger1 = true;
// get the location of finger 1
mouseX1 = x;
mouseY1 = y;
}
// same for these
if(id == 1){
finger2 = true;
mouseX2 = x;
mouseY2 = y;
}
if(id == 2){
finger3 = true;
mouseX3 = x;
mouseY3 = y;
}

if(id == 3){
finger4 = true;
mouseX4 = x;
mouseY4 = y;
}

if(id == 4){
finger5 = true;
mouseX5 = x;
mouseY5 = y;
}

}

}

 

//-----------------------------------------------------------------------------------------

// Override Processing's surfaceTouchEvent, which will intercept all

// screen touch events. This code only runs when the screen is touched.

 

 boolean surfaceTouchEvent(MotionEvent me) {

  // Number of places on the screen being touched:

  int numPointers = me.getPointerCount();

  for (int i=0; i < numPointers; i++) {

    int pointerId = me.getPointerId(i);

    float x = me.getX(i);

    float y = me.getY(i);

    

    // Added the .5 to make the outer ellipse noticeable

    float siz = me.getSize(i) + .5;

    infoCircle(x, y, siz, pointerId);

  }

  // If you want the variables for motionX/motionY, mouseX/mouseY etc.

  // to work properly, you'll need to call super.surfaceTouchEvent().

  return super.surfaceTouchEvent(me);

}

// everytime you touch the screen this is
// called and if you touch it with 2 
// fingers it is called twice
void touchStarted(){
// everytime you touch the screen fingers
// increases by 1 so if you touch it with 
// 2 fingers itll be 2
fingers = fingers + 1;
} 

// everytime you release a finger this is
// called so if you have 2 fingers on the
// screen then release them this will be 
// called twice
void touchEnded(){
// everytime its called fingers decreases 
// by 1 so working with the previous one
// this stabilizes it.
fingers = fingers - 1;

// this says you removed a finger so
// fingerRemoved = true
fingerRemoved = true;
}

// this is the main difference
void fingerRun(){

// because of the top things this says if 
// you have more than no fingers on the
// screen then yoy must be tapping it
if(fingers > 0){
tap = true;
}

// if you have no fingers on the screen
// then you arent tapping it
if(fingers == 0){
tap = false;
}

// this says if you remove a finger THEN 
// ALL FINGERS = 0!!! but dont worry the 
// upper code immediately switches it back
// the reason i did this is because theres
// no way to tell WHICH finger you removed
// so set all to 0 and immediately switch
// them back
if(fingerRemoved){
finger1 = false;
finger2 = false;
finger3 = false;
finger4 = false;
finger5 = false;
fingerRemoved = false;
}

}

```
