Generic mouse up/down detection

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Generic mouse up/down detection

Post by Fangbeast »

idle wrote:well you would be over active with those steaming undies ponging out your house, it's all that cycling running and kayaking though since Srod is always here it really makes one wonder if he's just imaging it all! :twisted:

I forgot what I was supposed to be saying!

No haven't eaten yet just been doing the beverages and building a Dll between mouthfuls.

So umm did that answer your question or would you prefer to slag of Srod for a few more posts, we know he's all good for the Antipodean abuse.
Yes, it worked fine.

And yes, slagging off srod and abusing him is always a good idea as he will feel neglected otherwise.

And as I always find sly references from him on the forums with my name in it, I must respond or he will bitch slap me with a dead ferret for not playing!!
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Generic mouse up/down detection

Post by srod »

And as I always find sly references from him on the forums with my name in it, I must respond or he will bitch slap me with a dead ferret for not playing!!
Nah, I've progressed to live ferrets now. Though they can be a little 'frisky' at times the little blighters. Makes a change from all the feral hamsters you keep sending me though. Removing the gaffer tape from one of your heavily abused hamsters is no fun believe me! And why do you insist on dressing them in your castoff gimp outfits? Have you any idea how difficult it is to remove all that leather and all those chains? Wait of course you have; I remember that photo of you and the kangaroo! Still gives me shivers!

:)
I may look like a mule, but I'm not a complete ass.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Generic mouse up/down detection

Post by Fangbeast »

Nah, I've progressed to live ferrets now.
You bastard!! idle was complaining that his crop seemed a little know and now we know where you've been pinching them yet again!
Though they can be a little 'frisky' at times the little blighters.
Then stop feeding them that strange moonshine that you strain through your jocks!!
Makes a change from all the feral hamsters you keep sending me though.
Now come on, that's not fair. You know you ordered them and I always deliver. I have the invoice to prove it!!
Removing the gaffer tape from one of your heavily abused hamsters is no fun believe me!
Now look here, if the order says "triple wrap", then who am I to argue with the customer??
And why do you insist on dressing them in your castoff gimp outfits?
I'm going to have to see my own supplier about this one as I distinctly said "castoff shrimp" to the mongrels!!.
Have you any idea how difficult it is to remove all that leather and all those chains?
Well, no. They are not designed to be removed of the slave inside will collapse!! (oops!)
Wait of course you have; I remember that photo of you and the kangaroo! Still gives me shivers! :)
Bastard!! Once again, you photoshopped yourself out of that drunken orgy picture. How could you??? And you looked so fetching in the pink mumu and crotchless leather bikie's helmet and the eel..err. that was an eel???
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
camille
User
User
Posts: 73
Joined: Tue Nov 19, 2019 12:52 pm

Re: Generic mouse up/down detection

Post by camille »

Sorry to dig up such an old thread :oops:

Can somebody show me a 100% reliable way to detect a left double click based on idles code?

I've tried various things with dblClickTime (by counting up the left button up and down messages)
and comparing the measured times between clicks with dblClickTime but I just don't get the logic right :(

I'd like the gadget text "Left double click + elapsed time between the two clicks" to appear, when two left mouse up events are detected and the time difference between them is <= dblClickTime. This must have some kind of priority over normal single clicks where the next one is > dblClickTime^^

Without this kind of detection I would get entries like
Left button up ... (this will be the first of two fast clicks!)
Left double click! ... (this is the second fast click!)

I know that there is another hook type that supports the event #WM_LBUTTONDBLCLK but if I understand that correctly, it needs to be written as a .dll file and cannot be a part of the normal code and I'd really like to avoid that...

Code: Select all

#WH_MOUSE_LL    = 14
#WM_MOUSEHWHEEL = 526

Structure MSLLHOOKSTRUCT
  pt.POINT
  mouseData.l
  flags.l
  time.l
  dwExtraInfo.l
EndStructure

Global myKeyHook.l

Procedure.l MouseProc(ncode.l, wParam.l, lParam.l)
  Protected.i xDist, yDist, dblClickTime = GetDoubleClickTime_()

  Static.i lbStarttime, lbEndtime, lbDiffTime, lbCntReleased, mbStarttime, mbEndtime, rbStarttime, rbEndtime

  Static xStart, xStop, yStart, yStop
  Protected px, py
  Static mMouseInput.MSLLHOOKSTRUCT
  CopyMemory(lparam, @mMouseInput, SizeOf(MSLLHOOKSTRUCT))
  Static mInput.MOUSEINPUT

  If ncode = #HC_ACTION
    If wParam
      Select wParam

        Case #WM_LBUTTONDOWN
          xStart = mMouseInput\pt\x
          yStart = mMouseInput\pt\y
          If lbCntReleased = 0
            lbDblClickStartTime = mMouseInput\time
          EndIf
          lbStartTime = mMouseInput\time
          ;AddGadgetItem(0, -1, "Left button down")


        Case #WM_LBUTTONUP
          lbCntReleased + 1
          xStop = mMouseInput\pt\x
          yStop = mMouseInput\pt\y
          If xStop >= xStart : xDist = xStop - xStart : Else : xDist = xStart - xStop : EndIf
          If yStop >= yStart : yDist = yStop - yStart : Else : yDist = yStart - yStop : EndIf
          lbEndtime = mMouseInput\time
          lbDiffTime = lbEndtime - lbStartTime

          If lbCntReleased = 2
            lbCntReleased = 0
            lbDblClickDiffTime = lbEndtime - lbDblClickStartTime

            If lbDblClickDiffTime <= dblClickTime
              AddGadgetItem(0, -1, "Left double click!" + Str(lbDblClickDiffTime) + " ms elapsed, moved x: " + xDist + " y: " + yDist + ")")
            Else
              AddGadgetItem(0, -1, "Left button up (" + Str(lbDiffTime) + " ms elapsed, moved x: " + xDist + " y: " + yDist + ")")
            EndIf
          Else
            If lbDiffTime >= dblClickTime
              lbCntReleased = 0
              AddGadgetItem(0, -1, "Left button up (" + Str(lbDiffTime) + " ms elapsed, moved x: " + xDist + " y: " + yDist + ")")
            EndIf
          EndIf


        Case #WM_MBUTTONDOWN
          xStart = mMouseInput\pt\x
          yStart = mMouseInput\pt\y
          mbStarttime = mMouseInput\time
          ;AddGadgetItem(0, -1, "Middle button down")


        Case #WM_MBUTTONUP
          xStop = mMouseInput\pt\x
          yStop = mMouseInput\pt\y
          mbEndtime = mMouseInput\time
          If xStop >= xStart : xDist = xStop - xStart : Else : xDist = xStart - xStop : EndIf
          If yStop >= yStart : yDist = yStop - yStart : Else : yDist = yStart - yStop : EndIf
          AddGadgetItem(0, -1, "Middle button up (" + Str(lbEndTime - lbStartTime) + " ms elapsed, moved x: " + xDist + " y: " + yDist + ")")


        Case #WM_RBUTTONDOWN
          xStart = mMouseInput\pt\x
          yStart = mMouseInput\pt\y
          rbStartTime = mMouseInput\time
          ;AddGadgetItem(0, -1, "Right button down")


        Case #WM_RBUTTONUP
          xStop = mMouseInput\pt\x
          yStop = mMouseInput\pt\y
          rbEndtime = mMouseInput\time
          If xStop >= xStart : xDist = xStop - xStart : Else : xDist = xStart - xStop : EndIf
          If yStop >= yStart : yDist = yStop - yStart : Else : yDist = yStart - yStop : EndIf
          AddGadgetItem(0, -1, "Right button up (" + Str(rbEndTime - rbStartTime) + " ms elapsed, moved x: " + xDist + " y: " + yDist + ")")


        Case #WM_MOUSEMOVE
          px = mMouseInput\pt\x
          py = mMouseInput\pt\y


        Case #WM_MOUSEWHEEL
          If mMouseInput\mouseData > 0
            ;Debug "up"
          Else
            ;Debug "down"
          EndIf
      EndSelect
    EndIf
  EndIf

  ProcedureReturn CallNextHookEx_(myMousehook, nCode, wParam, lParam)

EndProcedure

Procedure SetMouseHook()
  hInstance = GetModuleHandle_(0)

  If hInstance
    myMouseHook = SetWindowsHookEx_(#WH_MOUSE_LL, @MouseProc(), hInstance, 0)
  Else
    MessageRequester("hook", "Cannot get module handle")
  EndIf

EndProcedure

Procedure KillMouseHook()
  UnhookWindowsHookEx_(myMouseHook)
  MyMouseHook = 0
EndProcedure

If OpenWindow(0, 0, 0, 500, 300, "Mouse Test", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  SetMouseHook()
  EditorGadget (0, 10, 10, 480, 260)
  TextGadget(1, 10,  280, 250, 20, "Click in this here area down here")
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

KillMouseHook()
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Generic mouse up/down detection

Post by Mijikai »

This will return #True if 2 consecutive leftclicks are within the doubleclick timeframe:

Code: Select all

Procedure.i DoubleClick()
  Static dc_time.q
  Static dc_timestamp.q
  Protected dc_clock.q
  Protected dc_delay.q
  If dc_time = #Null
    dc_time = GetDoubleClickTime_()
  EndIf
  dc_clock = ElapsedMilliseconds()
  If dc_timestamp 
    dc_delay = dc_clock - dc_timestamp
    If dc_delay < dc_time
      dc_timestamp = #Null
      ProcedureReturn #True
    EndIf
  Else
    dc_timestamp = dc_clock
  EndIf
  ProcedureReturn #False
EndProcedure

Code: Select all

;...
Case #WM_LBUTTONDOWN
  If DoubleClick()
    ;...
  EndIf
;...
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4955
Joined: Sun Apr 12, 2009 6:27 am

Re: Generic mouse up/down detection

Post by RASHAD »

Hi

Code: Select all

.
.
Global myKeyHook.l ,start
.
.
.
Procedure.l MouseProc(ncode.l, wParam.l, lParam.l)
  Protected.i xDist, yDist, dblClickTime = GetDoubleClickTime_()

  Static.i lbStarttime, lbEndtime, lbDiffTime, lbCntReleased, mbStarttime, mbEndtime, rbStarttime, rbEndtime

  Static xStart, xStop, yStart, yStop
  Protected px, py
  Static mMouseInput.MSLLHOOKSTRUCT
  CopyMemory(lparam, @mMouseInput, SizeOf(MSLLHOOKSTRUCT))
  Static mInput.MOUSEINPUT

  If ncode = #HC_ACTION
    If wParam
      Select wParam

        Case #WM_LBUTTONDOWN
          xStart = mMouseInput\pt\x
          yStart = mMouseInput\pt\y
          If GetTickCount_() < (start + GetDoubleClickTime_())
            AddGadgetItem(0, -1, "Left button Dbl Click")
          Else
            AddGadgetItem(0, -1, "Left button down")
          EndIf
          start =  GetTickCount_()    
.
.

Egypt my love
camille
User
User
Posts: 73
Joined: Tue Nov 19, 2019 12:52 pm

Re: Generic mouse up/down detection

Post by camille »

Merci beaucoup, @Mijikai and @RASHAD!
Post Reply