Sorry to dig up such an old thread
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()