Python Forum
wxpython single click/double click recognition
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wxpython single click/double click recognition
#1
written on windows 7 using python 3.6.2, should work on any platform,
but not tested on Linux or OS-X

I've been doing a lot of playing around with wxpython for the past few weeks.
I needed a reliable routine that could distinguish between single and double
mouse clicks.

There is a good timer example in the demos that come with the wxpython phoenix
tarball, and I used this as the basis for this routine.

Here's how it works:
  • wx.EVT_LEFT_DOWN is bound to on_left_down
  • wx.EVT_LEFT_DCLICK is bound to on_dbl_click
  • wx.EVT_TIMER is bound to single_click

when a EVT_LEFT_DOWN event is triggered, the timer is started

if another left click down event is triggered (recognized as a  EVT_LEFT_DCLICK),
the timer is stopped because, it's no longer needed, this is a double click.

After 250ms EVT_TIMER event occurs, that means that the full 250ms elapsed and the
event must be a single click. The timer is stopped and right click can be processed.

Here's the code:
#
# Single, Double click recognition for wxpython using
# a timer event -- should be very reliable --
#
import wx


class TrySingleDouble(wx.Frame):
   def __init__(self, title):
       wx.Frame.__init__(self, None, title=title, size=(350, 200))

       # Delay time of 500ms, is what's used by Microsoft, but this is too long
       # and can be a lot shorter (especially if used for gaming). Here I
       # use 300 ms.
       self.dbl_clk_delay = 250

       self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
       self.Bind(wx.EVT_LEFT_DCLICK, self.on_dbl_click)
       self.Bind(wx.EVT_TIMER, self.single_click)

   def on_dbl_click(self, e):
       self.stop_timer()
       print('Double click')

   def on_left_down(self, e):
       self.start_timer()

   def start_timer(self):
       self.timer1 = wx.Timer(self)
       self.timer1.Start(self.dbl_clk_delay)

   def stop_timer(self):
       self.timer1.Stop()
       del self.timer1

   def single_click(self, e):
       self.stop_timer()
       print('Single_click')


def tst_app():
   app = wx.App(redirect=True)
   win = TrySingleDouble("Single-Double Click Test")
   win.Show()
   app.MainLoop()


if __name__ == '__main__':
   tst_app()
Reply
#2
I modified the time delay in my post to 300. I was able to beat the 250ms time delay, but
not 300. A younger person may still be able to beat this, if anyone tries this out and find's
this to be the case, please let me know.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Click Click snippsat 6 8,840 Apr-16-2017, 12:32 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020