How to prevent #PB_EventType_LeftClick msg at dblclick?

Everything else that doesn't fall into one of the other PB categories.
dige
Addict
Addict
Posts: 1416
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

How to prevent #PB_EventType_LeftClick msg at dblclick?

Post by dige »

If you click on the image gadget once, you'll get an
#PB_EventType_LeftClick .. thats good.
But if you doubleclick on theimage gadget, you'll get
#PB_EventType_LeftClick and #PB_EventType_LeftDoubleClick.

How to prevent that #PB_EventType_LeftClick first?

Code: Select all

;{- Enumerations / DataSections
;{ Windows
Enumeration
  #Window_0
EndEnumeration
;}
;{ Gadgets
Enumeration
  #Image_0
  #ListView_1
EndEnumeration
;}
;{ Images
Enumeration
  #Image_Image_0
EndEnumeration
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;}
Procedure OpenWindow_Window_0()
  If OpenWindow(#Window_0, 450, 200, 400, 181, "Window_0", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
    If CreateGadgetList(WindowID(#Window_0))
      ImageGadget(#Image_0, 13, 14, 180, 158, 0, #PB_Image_Border)
      ListViewGadget(#ListView_1, 211, 15, 151, 158)
    EndIf
  EndIf
EndProcedure
CreateImage(0, 180, 158)
OpenWindow_Window_0()

;{- Event loop
Repeat
  Event = WaitWindowEvent()
  Select Event
      ; ///////////////////
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      EventType = EventType()
      If EventGadget = #Image_0
        If EventType = #PB_EventType_LeftClick
          AddGadgetItem(#ListView_1, -1, "Left Click :-(")
        ElseIf EventType = #PB_EventType_LeftDoubleClick
          AddGadgetItem(#ListView_1, -1, "Left Double Click :-)")
        EndIf
      ElseIf EventGadget = #ListView_1
      EndIf
      ; ////////////////////////
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        Break
      EndIf
  EndSelect
ForEver
;
;}
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

I think this has been discussed before.

There are ways to ignore the singleclick, but I'm not sure we want to do away with it.

http://www.purebasic.fr/english/viewtop ... 81&start=3 - also has a link to an alternate :)

I actually have code that depends on it :)

cheers
Last edited by rsts on Tue Aug 11, 2009 5:22 pm, edited 1 time in total.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

LOL... :lol:

You write

Code: Select all

        If EventType = #PB_EventType_LeftClick 
          AddGadgetItem(#ListView_1, -1, "Left Click :-(") 
        ElseIf EventType = #PB_EventType_LeftDoubleClick 
          AddGadgetItem(#ListView_1, -1, "Left Double Click :-)") 
        EndIf 
But you could change the order for logical reasons. Try this

Code: Select all

        If EventType = #PB_EventType_LeftDoubleClick 
          AddGadgetItem(#ListView_1, -1, "Left Doueble Click :-)") 
        ElseIf EventType = #PB_EventType_LeftClick 
          AddGadgetItem(#ListView_1, -1, "Left Click Only :-)") 
        EndIf 
This is just an idea.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Post by cas »

AND51 wrote:This is just an idea.
You commented your own idea even before you said it, here it is :
AND51 wrote:LOL... :lol:
This is epic LOL :lol:

If you doubleclick on gadget then WaitWindowEvent() will return two times, first time left click and second time double click because in reality you actually click two times (two left clicks), and OS calculates time passed from last click to generate doubleclick event if you clicked fast enough second time, so no matter how you arrange your cases, they both will be processed.

There is probably no other way than coding custom timer calculation to check for doubleclick...



Here is my quick attempt, it is not perfect, feel free to enhance it 8)

Code: Select all

EnableExplicit
Global SysDoubleClickDelayMs = 500 ; According to Microsoft's MSDN website, the default timing in Windows is 500ms; change this value with some api which returns current sys doubleclick interval in milliseconds because users can edit this value in Control Panel

Global LastTimeOfLeftClick=-1
Global LeftClickOnlyProcessed=#True

Global lastMousePos_x=0
Global lastMousePos_y=0
Global currMousePos_x=0
Global currMousePos_y=0

;{- Enumerations / DataSections
;{ Windows
Enumeration
  #Window_0
EndEnumeration
;}
;{ Gadgets
Enumeration
  #Image_0
  #ListView_1
EndEnumeration
;}
;{ Images
Enumeration
  #Image_Image_0
EndEnumeration
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;}

Procedure CheckClick(void)
  Repeat
    If LastTimeOfLeftClick<>-1
      If (ElapsedMilliseconds()-LastTimeOfLeftClick)>SysDoubleClickDelayMs
        LeftClickOnlyProcessed=#False
        Repeat : Delay(1) : Until LeftClickOnlyProcessed=#True
      EndIf
    EndIf
    Delay(1)
  ForEver
EndProcedure

Procedure OpenWindow_Window_0()
  If OpenWindow(#Window_0, 450, 200, 400, 181, "Window_0", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
    ;If CreateGadgetList(WindowID(#Window_0))
      ImageGadget(#Image_0, 13, 14, 180, 158, 0, #PB_Image_Border)
      CreateThread(@CheckClick(),0)
      ListViewGadget(#ListView_1, 211, 15, 151, 158)
    ;EndIf
  EndIf
EndProcedure
CreateImage(0, 180, 158)
OpenWindow_Window_0()

;{- Event loop
Repeat
  Event = WaitWindowEvent(1)
  If Event=0
    If LeftClickOnlyProcessed=#False
      Event=#PB_Event_Gadget
      EventGadget = #Image_0
      EventType = #PB_EventType_LeftClick
    Else
      Continue
    EndIf
  EndIf
  
  Select Event
      ; ///////////////////
    Case #PB_Event_Gadget
      If LeftClickOnlyProcessed=#True
        EventGadget = EventGadget()
        EventType = EventType()
      EndIf
      If EventGadget = #Image_0
        If EventType = #PB_EventType_LeftClick
          currMousePos_x=WindowMouseX(#Window_0) 
          currMousePos_y=  WindowMouseY(#Window_0)
          
          If (currMousePos_x<>lastMousePos_x) Or (currMousePos_y<>lastMousePos_y)
            If LastTimeOfLeftClick<>-1
              LeftClickOnlyProcessed=#False
            EndIf
          EndIf
          
          lastMousePos_x=currMousePos_x
          lastMousePos_y=currMousePos_y
          
          LastTimeOfLeftClick=ElapsedMilliseconds()
          If LeftClickOnlyProcessed=#False
            AddGadgetItem(#ListView_1, -1, "Left Click :-(")
            LeftClickOnlyProcessed=#True
            LastTimeOfLeftClick=-1
          EndIf
          
        ElseIf EventType = #PB_EventType_LeftDoubleClick
          
          LastTimeOfLeftClick=-1
          AddGadgetItem(#ListView_1, -1, "Left Double Click :-)")
          LeftClickOnlyProcessed=#True
        EndIf
      ElseIf EventGadget = #ListView_1
      EndIf
      ; ////////////////////////
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        Break
      EndIf
  EndSelect
ForEver
;
;} 
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Post by Arctic Fox »

cas wrote:According to Microsoft's MSDN website, the default timing in Windows is 500ms; change this value with some api which returns current sys doubleclick interval in milliseconds because users can edit this value in Control Panel
For those on Windows you can use GetDoubleClickTime_()
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Post by cas »

@Arctic Fox:
Thanks, that's exactly what we need here.
dige
Addict
Addict
Posts: 1416
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Thank you all! Here is my solution, mixed and optimized all
suggestions (cas, netmeastro)

Code: Select all

Enumeration
  #Window_0
EndEnumeration
Enumeration
  #Image_0
  #ListView_1
EndEnumeration
Enumeration
  #Image_Image_0
EndEnumeration

Define.l Event, EventWindow, EventGadget, EventType, EventMenu

Procedure.i CheckDoubleClick ( EventGadgetID.i, EventType.i )
  Protected TimeOut.i
  
  If EventType = #PB_EventType_LeftClick
    TimeOut = ElapsedMilliseconds() + (GetDoubleClickTime_()*2/3)
    Repeat
      If WaitWindowEvent(10) = #PB_Event_Gadget And EventGadget() = EventGadget
        If EventType() = #PB_EventType_LeftDoubleClick
          EventType = #PB_EventType_LeftDoubleClick
          Break
        EndIf
      EndIf
    Until ElapsedMilliseconds() > TimeOut
  EndIf
  
  ProcedureReturn EventType
EndProcedure

Procedure OpenWindow_Window_0()
  If OpenWindow(#Window_0, 450, 200, 400, 181, "Window_0", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
    ImageGadget(#Image_0, 13, 14, 180, 158, 0, #PB_Image_Border)
    ListViewGadget(#ListView_1, 211, 15, 151, 158)
  EndIf
EndProcedure

CreateImage(0, 180, 158)
OpenWindow_Window_0()

Repeat
  Event = WaitWindowEvent()
  
  If Event = #PB_Event_Gadget And EventGadget() = #Image_0
    EventType = EventType()
    
    Select EventType
      Case #PB_EventType_LeftDoubleClick, #PB_EventType_LeftClick
        EventType = CheckDoubleClick( #Image_0, EventType )
        
        Select EventType
          Case #PB_EventType_LeftDoubleClick
            AddGadgetItem(#ListView_1, -1, "Left Double Click")
            
          Case #PB_EventType_LeftClick
            AddGadgetItem(#ListView_1, -1, "Left Click")
        EndSelect
    EndSelect
      
  ElseIf Event = #PB_Event_CloseWindow
    Break
  EndIf
ForEver
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: How to prevent #PB_EventType_LeftClick msg at dblclick?

Post by yrreti »

Thanks dije :D
I was beginning to pull my hair out on this problem, until I found
your example, which works really nice. I really like how you isolated the
Left Clicks and setup the CheckDoubleClick decode procedure.
Good job!
Thanks again
ozzie
Enthusiast
Enthusiast
Posts: 443
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: How to prevent #PB_EventType_LeftClick msg at dblclick?

Post by ozzie »

Yes, that could be useful. :)
Post Reply