Search code examples
pythonpython-3.xkivysingle-vs-double-tap

Python 3 & Kivy: React only to double tap "not" single tap


I hope you are all doing well in these strange times.

I have a question that has been asked before 4 years and 6 years ago, but neither answer in the thread works (at least for me), so in the hope that there has been some progress in the last years, I have to ask again.

When using Python 3.8.2 and Kivy 2.0.0 and running the below code (working snippet on my machine), on double-clicking/double-tapping the widget, both the single tap and the double tap registers.

How can I write this code in a way that only the single or only the double tap is registered? I just don't want both at the same time (I am actually not sure, why the out-of-the-box behaviour is desirable...)

In case it is important, I am on Windows 10 using anaconda, but I will port this code later to Raspbian.

Thanks a lot, folks. Really appreciate your input and stay healthy all of you :)

#!/usr/bin/env python

import kivy
kivy.require('2.0.0')

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

class RootWidget(BoxLayout):

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)

    def on_touch_down(self, touch):
        if touch.is_double_tap:
            print("Double Hello!")
        else:
            print("Single Hello!")

class TestApp(App):

    def build(self):
        root = RootWidget()
        return root


if __name__ == '__main__':
    TestApp().run()

Solution

  • Something like this:

    scheduled_event = None
    def on_touch_down(self, touch):
        if self.scheduled_event is not None:
                self.scheduled_event.cancel()
                self.scheduled_event = None
        if touch.is_double_tap:
                do_your_double_tap_thing()
        else:
            double_tap_wait_s = 1
            self.scheduled_event = Clock.schedule_once(do_your_single_tap_thing, double_tap_wait_s)