Distinguish Between Events

Everything else that doesn't fall into one of the other PB categories.
Worm
User
User
Posts: 20
Joined: Wed Apr 14, 2004 2:32 am
Location: Grand Rapids, MI
Contact:

Distinguish Between Events

Post 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?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

See EventType() in the manual
kawasaki
Enthusiast
Enthusiast
Posts: 182
Joined: Thu Oct 16, 2003 8:09 pm

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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
BERESHEIT
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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
BERESHEIT
Post Reply