applibgroup / HarmonyOS-Knowledgebase

This repository contains code samples of HarmonyOS Training
Apache License 2.0
15 stars 6 forks source link

How to detect double-tap in custom component in HarmonyOS? #65

Closed Click2cloud-Eros closed 3 years ago

Click2cloud-Eros commented 3 years ago

Describe the query

I am creating a custom component in HarmonyOS using Java SDK, Where I have to perform some task on double-tap. But I am not able to detect double-tap event.

In Android, with the help of GestureDetector class we can detect double-tap event as follow:

GestureDetector gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                return super.onSingleTapConfirmed(e);
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                return super.onDoubleTap(e);
            }
        });

In HMOS, I have tried to detect double-tap using Component.TouchEventListener as follow:

Component.TouchEventListener touchEventListener = new TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                if(touchEvent.getPointerCount() == 1) {
                    LogUtil.info(TAG, "single click detected");
                }

                if(touchEvent.getPointerCount() == 2) {
                    LogUtil.info(TAG, "double click detected");
                }
                return false;
            }
        };

but, It has not detected double-tap. On double-tap also it has printed single click detected.

Stackoverflow link here:

**** https://stackoverflow.com/questions/69086228/how-to-detect-double-tap-in-custom-component-in-harmonyos

Additional information

Developer Platform: Windows DevEco Studio version: 2.1 Beta 4 SDK API version: 5

GowthamRayar commented 3 years ago

try registering Component.DoubleClickedListener and you will be able to detect Double Tap in Custom Component, sample Usage

    customComponent.setDoubleClickedListener(new Component.DoubleClickedListener() {
                @Override
                public void onDoubleClick(Component component) {
                    LogUtil.info(TAG, "double click detected");
                }
            });
Click2cloud-Eros commented 3 years ago

Hi @GowthamRayar , Actually, I also want TouchEvent instance for getting pointer position to perfom my task. Sorry for wrong question. can I update my question OR create new issue with new question?