# Double click on Android

**URL:** <https://discourse.processing.org/t/double-click-on-android/5912>\
**Category:** Processing for Android\
**Created:** [November 25, 2018, 10:49am UTC](https://discourse.processing.org/t/double-click-on-android/5912 "2018-11-25T10:49:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [November 25, 2018, 10:49am UTC](https://discourse.processing.org/t/double-click-on-android/5912/1 "2018-11-25T10:49:42Z")

</div>

Hello,  
Processing can detect a double click on a phone ?  
If so, how to code it ?

Thank you.

---

<div class="post-metadata">

**Author:** ![brunoruchiga](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brunoruchiga/32/2235_2.png) [@brunoruchiga](https://discourse.processing.org/u/brunoruchiga)\
**Post date:** [November 25, 2018, 11:33am UTC](https://discourse.processing.org/t/double-click-on-android/5912/2 "2018-11-25T11:33:43Z")

</div>

You can use the Kentai Library:

[http://ketai.org](http://ketai.org)

[http://ketai.org/examples/gestures/](http://ketai.org/examples/gestures/)

[http://ketai.org/reference/ui/ketaigesture/](http://ketai.org/reference/ui/ketaigesture/)

---

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [November 25, 2018, 12:27pm UTC](https://discourse.processing.org/t/double-click-on-android/5912/3 "2018-11-25T12:27:27Z")

</div>

Thank you, I’m going to look.

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [November 25, 2018, 5:17pm UTC](https://discourse.processing.org/t/double-click-on-android/5912/4 "2018-11-25T17:17:23Z")

</div>

@hugo===  
“standard” way to do that is using android motionEvent() with sequence ActionDown+ActionUp+ ActionDown+ActionUp and millis(): you define time elapsed when it s double tap…

---

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [November 25, 2018, 5:56pm UTC](https://discourse.processing.org/t/double-click-on-android/5912/5 "2018-11-25T17:56:13Z")

</div>

Hello,  
Do you have an exemple code ?  
Thank you.

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [November 25, 2018, 6:27pm UTC](https://discourse.processing.org/t/double-click-on-android/5912/6 "2018-11-25T18:27:53Z")

</div>

@hugo===  
all details an examples here: [https://developer.android.com/reference/android/view/MotionEvent](https://developer.android.com/reference/android/view/MotionEvent)

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [November 26, 2018, 4:00am UTC](https://discourse.processing.org/t/double-click-on-android/5912/7 "2018-11-26T04:00:28Z")

</div>

You can use the common mouse event functions as well. This [post](https://forum.processing.org/two/discussion/17230/how-to-do-double-click) could show one possible approach.

Kf

---

<div class="post-metadata">

**Author:** ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)\
**Post date:** [November 26, 2018, 10:09am UTC](https://discourse.processing.org/t/double-click-on-android/5912/8 "2018-11-26T10:09:15Z")

</div>

I’ve been tinkering with this recently … I have code that does various gestures, but not pinch yet. It uses the Android Mode touchStarted(), touchEnded() and touchDragged() callbacks and 2 simple timing threads to determine the gesture type. You then handle the gesture in the functions that get called. It was my way of avoiding having to fathom the MotionEvent developer guide!

Hope it’s useful!

```auto

String touchText = "..." ;

void setup() {
  fullScreen() ;
  fill(255);
  textAlign(CENTER, CENTER) ;
  textSize(width/20) ;
}

void draw() {
  background(0);
  text(touchText, 0, height/4, width, height/2) ;
}

// a few variables ...
boolean dragDetected = false ;
boolean longPressDetected = false ;
int touchTime, tapCount, elapsedTime ; 

//run tapTimer() as a thread to count taps
void tapTimer() {
  // set time to wait for taps to happen
  // this avoids calling onSingleTap() if doubleTap() is wanted
  delay(200) ; 
  switch(tapCount) {
  case 1:
    onSingleTap() ;
    break;
  case 2:
    onDoubleTap() ;
    break;
  default:
    break;
  }
  tapCount = 0 ; //reset
} 

//run touchTimer as a thread to capture longPress gesture
void touchTimer() {
  longPressDetected = false ;
  while (!longPressDetected && !dragDetected) {
    elapsedTime = millis() - touchTime ;
    if (elapsedTime > 500) {
      longPressDetected = true ;
      onLongPress() ;
    }
  }
}

// touchStarted(), touchEnded() and touchMoved() are provided by Android Mode
//using then here to 

void touchStarted() {
  touchTime = millis() ;
  thread("touchTimer") ; //checks for longpress
  dragDetected = false ;
}

void touchEnded() {
  //check for longPress and drag and if neither have happened
  //start the tapTimer() thread to count taps
  if (longPressDetected) {
    //do nothing
  } else if (dragDetected) {
    if (millis()-touchTime < 150 ) { 
      onFlick();
    }
    tapCount = 0 ;
  } else {
    if (tapCount == 0) {   
      thread("tapTimer") ;
    }
    tapCount +=1 ;
  } 
  longPressDetected = true ; //leave touchTimer thread if it's running
}

void touchMoved() {
  touchText = "touch moved" ;
  dragDetected = true ;
  onDrag() ;
}

// the functions below are called from the above code 
// use them to handling gestures
// use touches[0].x and touches[0].y to determine screenlocation

void onSingleTap() {
  touchText = "single tap" ;
  println("single tap") ;
}

void onDoubleTap() {
  touchText = "double tap" ;
  println("double tap") ;
}

void onLongPress() {
  touchText = "long press" ;
  println("longpress") ;
}

void onFlick() {
  touchText = "flick" ;
  println("flick") ;
}

void onDrag() {  
  touchText = "drag" ;  
  println("drag") ;
}

```

---

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [November 26, 2018, 4:37pm UTC](https://discourse.processing.org/t/double-click-on-android/5912/9 "2018-11-26T16:37:26Z")

</div>

Hello,  
Thank you so much. 😀

---

<div class="post-metadata">

**Author:** ![technew](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/technew/32/6364_2.png) [@technew](https://discourse.processing.org/u/technew)\
**Post date:** [October 25, 2019, 2:33pm UTC](https://discourse.processing.org/t/double-click-on-android/5912/10 "2019-10-25T14:33:33Z")

</div>

For some reason, part of ketaiGestures (onTap, onLongpressed) are broken on the Pixel 4.  
I tested @ [shedMusic](https://discourse.processing.org/u/shedMusic) instructive codes above. Here’s my modified version.

```auto
    boolean longPressDetected = false;
    boolean locked = false;
    int sx = 0, sy = 0;
    int touchTime;

    public void touchStarted() {
        if(locked){return;}
        locked = true;

        sx = mouseX;
        sy = mouseY;

        touchTime = millis();
        new Thread(() -> {
            longPressDetected = false;
            while (!longPressDetected && locked) {
                if (millis() - touchTime >= 500) {
                    longPressDetected = true;
                    if (touches.length > 0 && dist(sx, sy, mouseX, mouseY) < 36) {
                        onLongPress(sx, sy);
                    }
                    locked = false;
                }
            }
        }).start();
    }

    public void touchMoved() {

    }

    public void touchEnded() {
        if (!longPressDetected && millis() - touchTime <= 250) {
            if (dist(sx, sy, mouseX, mouseY) < 36) {
                onTap(sx, sy);
            }
            locked = false;
        }
    }

    private void onTap(int x, int y) {
        //Todo 
    }

    private void onLongPress(int x, int y) {
        //Todo
    }

```

---

<div class="post-metadata">

**Author:** ![anon68884268](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon68884268](https://discourse.processing.org/u/anon68884268)\
**Post date:** [February 7, 2020, 6:12pm UTC](https://discourse.processing.org/t/double-click-on-android/5912/11 "2020-02-07T18:12:13Z")

</div>

On Smartwatch (Fossil Sport) out of nowhere it isn’t working. I replaced `touchStarted()` with `mousePressed()`, `touchEnded()` with `mouseReleased()` and `touchMoved()` with `mouseMoved()` and it works.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [February 9, 2020, 5:10pm UTC](https://discourse.processing.org/t/double-click-on-android/5912/12 "2020-02-09T17:10:44Z")

</div>

@technew @anon68884268 Could you please report this issue with the Ketai lib ownsers [here](https://github.com/ketai/ketai/issues) and share your bug report link here so ppl can follow it?

Kf
