Forum Archive

Handle double tap in TextView?

ywangd

Is it possible to handle double tap event in a TextView? Or somehow let the TextView pass the event to its parent CustomView?

JonB

You can do it with some ui.delays and delegates:

import ui, console
class MyTextViewDelegate (object):
    def __init__(self):
        self.tapstate=0
        self.tapdelay=0.5
    def textview_should_begin_editing(self, textview):
        ui.cancel_delays()
        if self.tapstate==0: # start timer to do normal edit
            self.tapstate=1
            def launch():
                self.tapstate=2
                textview.begin_editing()
            ui.delay(launch,self.tapdelay)
            return False 
        elif self.tapstate==1:  # do whatever you want here
            console.hud_alert('double tapped')
            self.tapstate = 0
            return False 
        elif self.tapstate==2 : # edit
            return True 

    def textview_did_begin_editing(self, textview):
        self.tapstate= 0 # rearm it
        pass


v=ui.View()
v.present()
t=ui.TextView(frame=(0,0,400,100),bg_color=(0.9,0.9,0.9))
t.delegate=MyTextViewDelegate()
v.add_subview(t)
ywangd

Thanks JonB. You are great.