Left mouse button hotkey - single, double click, click and hold? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
highend
Posts: 47
Joined: 24 Nov 2014, 16:57

Left mouse button hotkey - single, double click, click and hold?

Post by highend » 22 Jul 2016, 15:12

Hi,

I'd like to capture left mouse button clicks and determine if:
- Was it a single click
- Was it a double click (two consecutive clicks during the max. double click time -> DllCall("GetDoubleClickTime"))
- Was it a click and hold (the button wasn't released after clicking it)

The logic of my code is a bit off (it never get's to the double click tooltip) but single clicks and click and hold
is recognized. Click and hold will be used to be able to still select blocks of text. A major problem with click
and hold is that the KeyWait timeout is too long (0.15) so that fast click and holds don't select right from
the position where I clicked... Lowering the timeout (e.g. 0.05) makes the click and hold perfectly accurate but it won't
reach the single click part anymore...

Does anyone have some tips to get all parts working correctly?

Code: Select all

LButton::

	mbLeftClicks++

	; Check if mouse button get's released immediately
	KeyWait, %A_ThisHotkey%, T0.15
	if (ErrorLevel = 1) { ; Button is still down -> Click and hold
		ToolTip, Click and hold: %mbLeftClicks%
		Click down
		KeyWait, %A_ThisHotkey%
		if (ErrorLevel = 0) { ; Button was released
			mbLeftClicks := 0
			Click up
		}
	; Short single / double click(s)
	} else {
		; Double click
		if (mbLeftClicks = 2) {
			ToolTip, Double click: %mbLeftClicks%
			mbLeftClicks := 0
			Click 2

		; Single click
		} else {
			ToolTip, Single click: %mbLeftClicks%
			Click
			mbLeftClicks--
		}
	}
return

Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Left mouse button hotkey - single, double click, click and hold?

Post by Shadowpheonix » 25 Jul 2016, 12:39

I think this does what you want...

Code: Select all

LButton::
	; Check if mouse button get's released immediately
	KeyWait, %A_ThisHotkey%, T0.15
	if (ErrorLevel = 1) { ; Button is still down -> Click and hold
		ToolTip, Click and hold
		Click down
		KeyWait, %A_ThisHotkey%
		Click up
	}
	; Short single / double click(s)
	else {
		; Single click
		ToolTip, Single click
		Click
		; Double click
		if (A_TimeSincePriorHotKey < DllCall("GetDoubleClickTime") && A_PriorHotkey = A_ThisHotkey)
			ToolTip, Double click
	}
return

highend
Posts: 47
Joined: 24 Nov 2014, 16:57

Re: Left mouse button hotkey - single, double click, click and hold?

Post by highend » 26 Jul 2016, 00:25

Thank you Shadowpheonix!

It recognizes all three click variants perfectly :)

Unfortunately, due to the "long" KeyWait time (T0.15), click and hold must be used very carefully.
Left clicking somewhere and immediately moving the mouse over several lines (e.g. to select text in a webbrowser)
leads to left out characters because the click isn't instant but "lags a bit behind".

Lowering the timeout to 0.10 or even 0.05 leads to recognize nearly every single click as a "click and hold".

Is there anything that can be done to make the routine more reliable with very low KeyWait timeouts?
Maybe a loop that checks for a "physical mouse button down"?

User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Left mouse button hotkey - single, double click, click and hold?

Post by Nextron » 26 Jul 2016, 03:13

Is there a reason you're blocking the mouse clicks only to resend them artificially (with the delay)? Passing them through would help otherwise:

Code: Select all

~LButton::
	; Check if mouse button get's released immediately
	KeyWait, LButton, T0.15
	if (ErrorLevel = 1) { ; Button is still down -> Click and hold
		ToolTip, Click and hold
		;Click down
		KeyWait, LButton
		;Click up
	}
	; Short single / double click(s)
	else {
		; Single click
		ToolTip, Single click
		;Click
		; Double click
		if (A_TimeSincePriorHotKey < DllCall("GetDoubleClickTime") && A_PriorHotkey = A_ThisHotkey)
			ToolTip, Double click
	}
return

highend
Posts: 47
Joined: 24 Nov 2014, 16:57

Re: Left mouse button hotkey - single, double click, click and hold?

Post by highend » 26 Jul 2016, 03:22

Is there a reason you're blocking the mouse clicks only to resend them artificially (with the delay)?
Sure :) This hotkey will be surrounded by #IfWinActive statements and each recognized double click will send a script (by using WM_COPYDATA) to that application.
The whole thing will be combined by using keyboard modifiers as well (to allow "different" double click functions depending on which modifier is used) so in the end I would use:

*LButton:
...
return

and a few statements at the beginning for getting the key states for ctrl, shift and alt like:
isCtrlPressed := GetKeyState("Ctrl")
etc.

Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Left mouse button hotkey - single, double click, click and hold?  Topic is solved

Post by Shadowpheonix » 26 Jul 2016, 09:51

Does this variation solve your issue?

Code: Select all

*LButton::
Click Down  	; If you do not want the Ctrl, Shift, or Alt keys included as part of the click, change this to "Send {Click Down}"
	; Check if mouse button get's released immediately
	KeyWait, LButton, T0.15
	if (ErrorLevel = 1) { ; Button is still down -> Click and hold
		ToolTip, Click and hold
		KeyWait, LButton
	}
	; Short single / double click(s)
	else {
		; Single click
		ToolTip, Single click
		; Double click
		if (A_TimeSincePriorHotKey < DllCall("GetDoubleClickTime") && A_PriorHotkey = A_ThisHotkey)
			ToolTip, Double click
	}
Click Up  	; If you changed "Click Down" above, then change this to "Send {Click Up}"
return

highend
Posts: 47
Joined: 24 Nov 2014, 16:57

Re: Left mouse button hotkey - single, double click, click and hold?

Post by highend » 27 Jul 2016, 12:14

Yeah, it works :)

Thanks a lot, Shadowpheonix!

onidarbe
Posts: 10
Joined: 18 Feb 2020, 16:32

Re: Left mouse button hotkey - single, double click, click and hold?

Post by onidarbe » 18 Feb 2020, 16:41

I'm also trying to capture single, double and click-and-hold. But there is still a problem with this script:
It fires single clip before a double click :(

Code: Select all

	*RButton::
		KeyWait, RButton, T0.15	; Check if mouse button get's released immediately
		if (ErrorLevel = 1){ ; Button is still down
			sendinput {MButton down}
			KeyWait, RButton
			sendinput {MButton up}
		}
		else{
			if (A_TimeSincePriorHotKey < DllCall("GetDoubleClickTime") && A_PriorHotkey = A_ThisHotkey){		; Double click
				ToolTip, Double click
			}
			else{
				ToolTip, Single click
			}
		}
	return


onidarbe
Posts: 10
Joined: 18 Feb 2020, 16:32

Re: Left mouse button hotkey - single, double click, click and hold?

Post by onidarbe » 20 Feb 2020, 15:36

Found out myself ;)
Not only do I capture Single, Double and click and hold, I even have a Double click and hold.

I only wonder how to put DllCall("GetDoubleClickTime") is SetTimer, _SketchUpRButton, .....

Code: Select all

#IfWinActive, i).*SketchUp .*	;needs  SetTitleMatchMode, RegEx
{
 	*RButton::
		SketchUpRButton:=1
		if (A_TimeSincePriorHotKey < DllCall("GetDoubleClickTime") && A_PriorHotkey = A_ThisHotkey)		; Double click
			SketchUpRButton:=2
		else
			SetTimer, _SketchUpRButton, 300
	return
return
}
_SketchUpRButton:
{
	SetTimer, _SketchUpRButton, off
	if GetKeyState("RButton", "P")
		SketchUpRButton:=SketchUpRButton+2	;add click and hold
	if SketchUpRButton=1									;single click
		ToolTip, Single click
	if SketchUpRButton=2									;double click
		ToolTip, Double click
	if SketchUpRButton=3									;single click and hold
		ToolTip, Single click and hold
	if SketchUpRButton=4									;double click and hold
		ToolTip, Double click and hold
	SketchUpRButton:=
return

Post Reply

Return to “Ask for Help (v1)”