Forum Archive

How to implement long button presses?

throwaway4

I'm trying to implement a long button press and record the elapsed time. Similar to how you can implement the touch_began and touch_ended methods for a ui.View, is it possible to do this for buttons?
I noticed that if I create a view with a button that is touch-enabled, I can tap the screen to record a touch for any location EXCEPT the button.

Is there a simple way to do the same with buttons?

My code so far:

from datetime import datetime as dt
import ui

class Touch_Test(ui.View):
   def __init__(self):
      self.background_color = 'white'
      self.button = ui.Button(title='Button', frame=(100,100,100,30), action=self.get_touch)
      self.add_subview(self.button)
      self.present('fullscreen')

   def touch_began(self, touch):
      self.press_time = dt.now()
      print('Pressed {}'.format(touch.location))

   def touch_ended(self, touch):
      self.press_time_elapsed = dt.now() - self.press_time
      print('Time Elapsed: {}'.format(self.press_time_elapsed))

   def get_touch(self, sender):
      print('Pressed Button {}'.format(sender)

if __name__ == '__main__':
   t = Touch_Test()
cvp

@throwaway4 see gestures of @mikael