Double click on Android

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

Thank you.

You can use the Kentai Library:

http://ketai.org

http://ketai.org/examples/gestures/

http://ketai.org/reference/ui/ketaigesture/

1 Like

Thank you, I’m going to look.

@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…

1 Like

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

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

1 Like

You can use the common mouse event functions as well. This post could show one possible approach.

Kf

1 Like

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!


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") ;
}
3 Likes

Hello,
Thank you so much. :grinning:

For some reason, part of ketaiGestures (onTap, onLongpressed) are broken on the Pixel 4.
I tested @ shedMusic instructive codes above. Here’s my modified version.

    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
    }
2 Likes

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.

@technew @anon68884268 Could you please report this issue with the Ketai lib ownsers here and share your bug report link here so ppl can follow it?

Kf