Attempted "middle click emulation", now can't click and hold with left of right mouse buttons

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CrimsonTears
Posts: 2
Joined: 26 Feb 2019, 21:01

Attempted "middle click emulation", now can't click and hold with left of right mouse buttons

26 Feb 2019, 21:34

I'm trying to copy a feature in some linux distros that emulates a middle click when the right and left click buttons are pressed simultaneously (or very close to) and this is what I've got so far. The first two sections are just to make the right and left click function normally, and they do, but when the last two sections are present, Pressing and holding either the left or right button (when dragging for example) doesn't work. I think this is because it's waiting for the other button to be pressed for a middle click but i'm not sure how to work around this. Plz help :angel:

Code: Select all

LButton::
MouseClick, left,,,,, D
KeyWait, LButton
MouseClick, left,,,,, U
return

RButton::
MouseClick, right,,,,, D
KeyWait, RButton
MouseClick, right,,,,, U
return

RButton & LButton::
MouseClick, middle,,,,, D
KeyWait, LButton
MouseClick, middle,,,,, U
return

LButton & RButton::
MouseClick, middle,,,,, D
KeyWait, LButton
MouseClick, middle,,,,, U
return
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Attempted "middle click emulation", now can't click and hold with left of right mouse buttons

26 Feb 2019, 23:21

This seems to work pretty well

Code: Select all

#if getKeyState("LButton","P") && getKeyState("RButton","P")
$LButton::
$RButton::
t:=1
mouseClick,middle,,,,,d
hotkey,~$LButton up,bu,on
hotkey,~$RButton up,bu,on
return

#if
bu:
hotkey,% a_thisHotkey,off
if(t){
    mouseClick,middle,,,,u
    t:=0
}
return
Bit complicated. Here's a simplified version, with potentially more issues.

Code: Select all

#if getKeyState("LButton","P") && getKeyState("RButton","P")
$LButton::
$RButton::
mouseClick,middle,,,,,d
while(getKeyState("LButton","P") && getKeyState("RButton","P"))
    sleep 10
mouseClick,middle,,,,,u
return
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
CrimsonTears
Posts: 2
Joined: 26 Feb 2019, 21:01

Re: Attempted "middle click emulation", now can't click and hold with left of right mouse buttons

27 Feb 2019, 11:56

Masonjar13 wrote:
26 Feb 2019, 23:21
This seems to work pretty well

Code: Select all

#if getKeyState("LButton","P") && getKeyState("RButton","P")
$LButton::
$RButton::
t:=1
mouseClick,middle,,,,,d
hotkey,~$LButton up,bu,on
hotkey,~$RButton up,bu,on
return

#if
bu:
hotkey,% a_thisHotkey,off
if(t){
    mouseClick,middle,,,,u
    t:=0
}
return
Bit complicated. Here's a simplified version, with potentially more issues.

Code: Select all

#if getKeyState("LButton","P") && getKeyState("RButton","P")
$LButton::
$RButton::
mouseClick,middle,,,,,d
while(getKeyState("LButton","P") && getKeyState("RButton","P"))
    sleep 10
mouseClick,middle,,,,,u
return
Thanks a lot for that. Middle click dragging isn't quite working properly in either of those which of course isn't something most people will ever want to do but unfortunately I do that a lot in AutoCAD. The one I tried in the OP has the middle click working perfectly but I can't see how to make it work the same way in yours.
User avatar
evilC
Posts: 4824
Joined: 27 Feb 2014, 12:30

Re: Attempted "middle click emulation", now can't click and hold with left of right mouse buttons

28 Feb 2019, 06:03

None of above the solutions that use a blocking wait (KeyWait or GetKeyState loop) will work, because of the way AHK threading works.

Code: Select all

~LButton::
KeyWait, LButton
return

~RButton::
KeyWait, RButton
Tooltip RB RELEASED
return
Hold RB, Hold LB, then release RB - you get no msgbox until you release LB.

You must use SetTimer instead.
However, you probably want to think a bit more carefully about the logic used here - when dealing with a situation like this where you do not know how to respond to a key event until the future (eg when RB goes down, you don't know whether you want to send a right click until you have waited to see if LB goes down before the timeout expires), so you should block RB until that point.
This is not too big of a deal, but I would warrant that if you were blocking every LB down event for that timeout period, the left mouse would feel sluggish.
So I think maybe the best solution here would be to mandate that RB must be pushed before LB (Because imposing a delay on RB down is not likely to be an issue - in fact, context menus do not show until you *release* RB.
User avatar
evilC
Posts: 4824
Joined: 27 Feb 2014, 12:30

Re: Attempted "middle click emulation", now can't click and hold with left of right mouse buttons

28 Feb 2019, 06:24

Here's a stab at the implementation I suggested:

Code: Select all

#SingleInstance force
OutputDebug DBGVIEWCLEAR

Timeout := 200

TimerRunning := 0
Holding := 0
RBPressed := 0
WaitingForRBRelease := 0

RButton::
	SetTimer, WaitForLb, % "-" Timeout
	TimerRunning := 1
	return

RButton up::
	SetTimer, WaitForLb, Off
	TimerRunning := 0
	if (RBPressed){
		if (WaitingForRBRelease){
			WaitingForRBRelease := 0
			OutputDebug % "AHK| RB release after MB release - reset back to normal @ " A_TickCount
		} else {
			OutputDebug % "AHK| Send RB up @ " A_TickCount
			Send {Blind}{Rbutton up}
		}
	}
	return

LButton::
	if (TimerRunning){
		TimerRunning := 0
		SetTimer, WaitForLb, Off
		OutputDebug % "AHK| MB PRESS @ " A_TickCount
		Holding := 1
		Send {Blind}{Mbutton down}
	} else {
		;~ Send {Blind}{LButton down}
		OutputDebug % "AHK| Passing through LB down @ " A_TickCount
		Send {Blind}{LButton down}
	}
	return

LButton up::
	if (Holding){
		OutputDebug % "AHK| MB RELEASE @ " A_TickCount
		Holding := 0
		WaitingForRBRelease := 1
		Send {Blind}{Mbutton up}
	} else {
		OutputDebug % "AHK| Passing through LB up @ " A_TickCount
		Send {Blind}{Lbutton up}
	}
	return

WaitForLb:
	OutputDebug % "AHK| Send RB press @ " A_TickCount
	TimerRunning := 0
	RBPressed := 1
	Send {Blind}{Mbutton down}
	return

^Esc::ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Marium0505 and 343 guests