Fanning Software Consulting

Detecting Double Button Clicks

QUESTION: I was wondering if anyone had code or hints on how to process double clicks on a mouse button within an IDL event handler?

ANSWER: Here is a piece of code that can reliably detect a double click on a button widget, at least on my Macintosh and Windows NT machines.

Here is how it works. The first click on the button sets a "firstClick" flag and sends a TIMER event to the button event handler. If a second button click event gets to the button event handler before the TIMER event does, then this is a double click on the button! If the TIMER event gets to the button event handler before another button event, then this is a single button click.

You may need to experiment with the timer delay on your machine. I find a timer delay of 0.2 seconds works for my two machines. Click here to download the example program.

   PRO EXAMPLE_EVENT,event
   WIDGET_CONTROL, event.top, /DESTROY
   END ; of EXAMPLE_EVENT event handler **************************************


   PRO DOUBLE_CLICK_DETECT, event
   WIDGET_CONTROL, event.top, GET_UVALUE=info, /NO_COPY
   thisEvent = TAG_NAMES(event, /Structure)

      ; Double click event.

   IF (thisEvent EQ 'WIDGET_BUTTON') AND (info.firstClick EQ 1) THEN BEGIN
      WIDGET_CONTROL, info.textID, SET_VALUE="Hooray, that's a DOUBLE click!"
      info.firstClick = 0
      WIDGET_CONTROL, event.top, SET_UVALUE=info, /NO_COPY
      RETURN
   ENDIF

      ; Initial click event.

   IF (thisEvent EQ 'WIDGET_BUTTON') AND (info.firstClick EQ 0) THEN BEGIN
      info.firstClick = 1
      WIDGET_CONTROL, info.textID, SET_VALUE=''
      WIDGET_CONTROL, event.id, Timer=info.delay
   ENDIF

      ; Single click event.

   IF (thisEvent EQ 'WIDGET_TIMER') AND (info.firstClick EQ 1) THEN BEGIN
      WIDGET_CONTROL, info.textID, SET_VALUE="No, that's a SINGLE click!"
      info.firstClick = 0
   ENDIF

   WIDGET_CONTROL, event.top, SET_UVALUE=info, /NO_COPY
   END ; of DOUBLE_CLICK_DETECT event handler *******************************


   PRO EXAMPLE, delay
   IF N_PARAMS() EQ 0 THEN delay = 0.2
   info = {firstClick:0, delay:delay, textID:0L}
   tlb = WIDGET_BASE(ROW=2, TITLE='Double Click Example', $
      BASE_ALIGN_CENTER=1)
   info.textID = WIDGET_TEXT(tlb, Value='Button Messages Here...', $
      SCR_XSIZE=250)
   button = WIDGET_BUTTON(tlb, Value='Double Click Me...', $
      EVENT_PRO='DOUBLE_CLICK_DETECT')
   quit = WIDGET_BUTTON(tlb, Value='Quit')
   WIDGET_CONTROL, tlb, /REALIZE, SET_UVALUE=info, /NO_COPY
   XMANAGER, 'example', tlb
   END ; of EXAMPLE ********************************************************

Note that you can detect double clicks on list widgets by examining the event.click field of the event structure. A double click is indicated by the value 2 instead of 1. You can detect double clicks in draw widgets by using a modified version of the program above.

Google
 
Web Coyote's Guide to IDL Programming