Page 1 of 1

Distinguish Between Events

Posted: Wed Feb 28, 2007 5:50 pm
by Worm
I've got an image gadget and would like to have things happen both on Left Mouse Click, and Left Mouse Double Click.

Using an Select or If in my events loop, the Left Click fires on the first click. This makes sense, but how do I allow the Left Double Click to fire if its an actual Double Click and not the Left Click?

Posted: Wed Feb 28, 2007 8:18 pm
by Trond
See EventType() in the manual

Posted: Wed Feb 28, 2007 8:19 pm
by kawasaki
Use the EventType() command;

Code: Select all

Select EventGadget()

Case 1 ; Your Image Gadget

Select EventType()

Case #PB_EventType_LeftClick

; ....

Case #PB_EventType_LeftDoubleClick

; ....

Endselect
Endselect

Edit: haha trond beat me to it by 1 minute.

Posted: Wed Feb 28, 2007 9:05 pm
by netmaestro
Is it that you want to do two different things based on single or double click and you don't want the single click to register if it's actually a double click? If that's the case, here's a method to accomplish it. There is a downside: your reaction to a single click is slowed down by waiting to see if it's going to change to a doubleclick. In my example I've speeded up the wait to 2/3 of the user's doubleclick rate to make it a bit snappier for single click events:

Code: Select all

 CreateImage(0,64,64,32)
 OpenWindow(0, 0, 0, 200, 105, "ImageGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
 CreateGadgetList(WindowID(0))
 ImageGadget(1, 60, 10, 100, 83, ImageID(0), #PB_Image_Border)     

 Repeat 
  ev = WaitWindowEvent()
  Select ev 
    Case  #PB_Event_Gadget
      gadget = EventGadget()
      Select EventType()
        Case #PB_EventType_LeftClick
          detectedevent = #PB_EventType_LeftClick ; Start with single click but don't report yet
          ;
          ; Enter wait state to see if it'll change to a doubleclick event
          start = ElapsedMilliseconds()
          Repeat
            tmp = WaitWindowEvent(1)
            If tmp = #PB_Event_Gadget And EventGadget() = gadget 
              If EventType() = #PB_EventType_LeftDoubleClick
                detectedevent = #PB_EventType_LeftDoubleClick
                Break ; Got the changed event, no need to keep waiting
              EndIf
            EndIf
          Until ElapsedMilliseconds()-start >= GetDoubleClickTime_()/3*2
          ;
          Debug detectedevent ; 0=single, 2=double
   EndSelect
 EndSelect
 Until ev = #PB_Event_CloseWindow

Posted: Wed Feb 28, 2007 10:01 pm
by netmaestro
Also just found a similar solution with a different method by the mighty srod here:

http://www.purebasic.fr/english/viewtop ... 41&start=7