How to implement mouse single click & double click events

Windows specific questions.
Post Reply
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

How to implement mouse single click & double click events

Post by kcvinu »

Hi all,
I am playing with some win32 api gui code. Now, i would like to create a mouse click & double click event for my gui library. I've read that one would need to deal with four type of mouse messages to implement this feature.
For a single click -
WM_LBUTTONDOWN
WM_LBUTTONUP

And for a double click --
WM_LBUTTONDOWN
WM_LBUTTONUP
WM_LBUTTONDOWN
WM_LBUTTONUP

And if you set the CS_DBLCLK style in your window class--
WM_LBUTTONDOWN
WM_LBUTTONUP
WM_LBUTTONDBLCLK
WM_LBUTTONUP
I've also read that one would need to enable a timer and get the mouse pointer location to handle these messages. So these are my questions--
1. Is there a better way to do this ?
2. Assume that if i started a timer and collect the mouse position in WM_LBUTTONDOWN, then, Can i call my leftBtnDown event at that space ? I mean, i can elaborate the idea with this code
This is a part of my window type

Code: Select all


' these are events
leftBtnDown As UINT
leftBtnUp As UINT
leftClick As UINT
leftDblClick As UINT

' these are event handling function pointers
' this is just pseudo code, and only for expressing my idea
onLeftDown As function Ptr
onLeftUp As function Ptr
onLeftClick As function Ptr
onLeftDblClick As function Ptr

Code: Select all

case WM_LBUTTONDOWN
	'start a timer
	'collect the mouse position and stores it in a variable
	'set a boolean variable
	'So, after these three steps, can i call my event function here or just finish processing this message
	' like
	onLeftDown(e As EventArgs)
	
case WM_LBUTTONUP
	' Here, i need to check the timer's time. But i dont know which is the maximum allowed interval time between mouse down and up
	' If i call my mouse click event function here, then, can i call my left button up event function here ?
	if conditions are true then
		onLeftClick(e As EventArgs)
	end if
	onLeftUp(e As EventArgs) 
 
I hope i made everything clear. If not, please note here. I will try to explain a little more. Thanks in advance
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: How to implement mouse single click & double click events

Post by paul doe »

Indeed, that's in a nutshell how you implement it: handle the first mouse down message, cache the time passed between it and another mouse down message, and trigger a double click if the elapsed time is within a certain threshold (this is done usually when you handle the second mouse up message). Also, the deltas for the mouse position are cached too at the first message and compared to the ones from the second message, so as to provide certain 'tolerance' (ie it will be considered a double click even if the mouse pointer moved a 'pixel or two'; which is essential on some devices such as laptops).

As for the event order, they'll usually come in like this:

Code: Select all

'' For a Click event
MouseDown
MouseUp
Click

'' For a Double Click event
MouseDown
MouseUp
Click
MouseDown
MouseUp
DoubleClick
And about the time threshold and tolerances, you can either query these values from the OS, or simply use your own. It's up to you.
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to implement mouse single click & double click events

Post by kcvinu »

@paul doe ,
Thanks. :)
Post Reply