AHK has problem detecting LMB releasing

Ask gaming related questions (AHK v1.1 and older)
byzod
Posts: 87
Joined: 21 Jun 2021, 06:46

AHK has problem detecting LMB releasing

Post by byzod » 03 Jul 2022, 03:18

The Goal:
Hold down LMB to auto click LMB itself, stop when released LMB

The problem:

Sometimes the script is "trapped" in click loop even when I released the LMB

The code:

Code: Select all

~$LButton::
{
	Sleep 100
	Loop {
		Sleep 80
		if GetKeyState("LButton", "P")
			Click
		else
			break
	}
	Return
}

More Info:
I made this for a game (undermine, more specifically) that don't have auto attack feature
It works, but stuck in auto clicking 1~2 times per 5 minutes, it didn't stop when I released the LMB
Since the key state is checked every loop, it means that ahk think the LMB key is still pressed physically when I released it

p.s. I tested an alternative version but "click stuck" still happens, also 1~2 times per 5 minutes or so

Code: Select all

~$LButton::
{
	global UnderMine_IsAutoAtk := true
	SetTimer, MineClick, 100
	
	MineClick() {
		if(UnderMine_IsAutoAtk){
			Click
		} else {
			SetTimer, MineClick, off
		}
	}
	Return
}

~$LButton Up::
{
	UnderMine_IsAutoAtk := false
	Return
}

I'm not sure what cause this bug, hardware or the game or ahk itself
Any help is appreciated ❤


[Mod action: Moved topic to “Gaming”]

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: AHK has problem detecting LMB releasing

Post by mikeyww » 03 Jul 2022, 05:05

Test in Notepad.

Code: Select all

LButton::
While GetKeyState("LButton", "P") {
 Click
 Sleep, 80
}
Return

byzod
Posts: 87
Joined: 21 Jun 2021, 06:46

Re: AHK has problem detecting LMB releasing

Post by byzod » 03 Jul 2022, 05:15

mikeyww wrote:
03 Jul 2022, 05:05
Test in Notepad.

Code: Select all

LButton::
While GetKeyState("LButton", "P") {
 Click
 Sleep, 80
}
Return
Thanks for you code

I tested it for few minutes and there're 2 problem with it
1. The sleep is in the loop body so when I released the LMB, game character still swing one more time, as in this game you can't move when you're attacking, so this is an critical flaw for survival
2. The "clicking trap" still happens, 2 times in my 3 minutes test :(

byzod
Posts: 87
Joined: 21 Jun 2021, 06:46

Re: AHK has problem detecting LMB releasing

Post by byzod » 03 Jul 2022, 05:16

mikeyww wrote:
03 Jul 2022, 05:05
Test in Notepad.

Code: Select all

LButton::
While GetKeyState("LButton", "P") {
 Click
 Sleep, 80
}
Return
I tested again with your (and mine) code in firefox and reproduced this bug
I think it's not caused by the game, maybe moderator could please move this post to the correct forum?

step I used to reproduce it:

1. Open firefox with any page that you can observe click result, for example "demo.html" I used
demo.zip
(722 Bytes) Downloaded 16 times
2. Open a lot of tabs or few js-heavy site to make firefox busy (this step might be unnecessary, I don't know)
3. Run code below

Code: Select all

#IfWinActive ahk_exe firefox.exe
LButton::
{
	While GetKeyState("LButton", "P") {
		Click
		Sleep, 80
	}
	Return
}
#IfWinActive
4. Hold LMB on the button, release it after few seconds, repeat few minutes

expected result:
Auto-click always stop when you release LMB

actual result:
Occasionally, the auto-click continuous after you released LMB, seems like "stucked"
Last edited by byzod on 03 Jul 2022, 05:46, edited 1 time in total.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: AHK has problem detecting LMB releasing

Post by mikeyww » 03 Jul 2022, 05:41

What the script does:
Hold down LMB to auto click LMB itself, stop when released LMB
Regarding
ahk think the LMB key is still pressed physically when I released it
... this seems unlikely, though "overloading" a buffer can have unwanted effects. You can increase the sleep.

To learn more about "what the keys are doing", examine the :arrow: KeyHistory. That can inform adjustments.

AHK v1 does not use braces as bounding symbols for hotkey routines. I recommend removing those extraneous outer braces.

Your Notepad test had what result?

byzod
Posts: 87
Joined: 21 Jun 2021, 06:46

Re: AHK has problem detecting LMB releasing

Post by byzod » 03 Jul 2022, 06:28

mikeyww wrote:
03 Jul 2022, 05:41
What the script does:
Hold down LMB to auto click LMB itself, stop when released LMB
Regarding
ahk think the LMB key is still pressed physically when I released it
... this seems unlikely, though "overloading" a buffer can have unwanted effects. You can increase the sleep.

To learn more about "what the keys are doing", examine the :arrow: KeyHistory. That can inform adjustments.

AHK v1 does not use braces as bounding symbols for hotkey routines. I recommend removing those extraneous outer braces.

Your Notepad test had what result?
Reproduced in notepad.exe after 2 minutes, removed braces
I think it's the problem of my mouse or ahk itself, the application is irrelevant

Code: Select all

#IfWinActive ahk_exe notepad.exe
LButton::
	While GetKeyState("LButton", "P")
		Click
		Sleep, 80
	return
#IfWinActive
Reproduce step similar to firefox one
An1.gif
An1.gif (351.29 KiB) Viewed 1861 times
Last edited by byzod on 03 Jul 2022, 06:43, edited 1 time in total.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: AHK has problem detecting LMB releasing

Post by mikeyww » 03 Jul 2022, 06:31

I didn't suggest removing all braces, but just the outer bounding ones. Once fixed, would test again in Notepad. The sleep matters, but in the case of your script, there is no sleep during the While statement without a block.

Explained: Block

byzod
Posts: 87
Joined: 21 Jun 2021, 06:46

Re: AHK has problem detecting LMB releasing

Post by byzod » 03 Jul 2022, 06:48

mikeyww wrote:
03 Jul 2022, 06:31
I didn't suggest removing all braces, but just the outer bounding ones. Once fixed, would test again in Notepad. The sleep matters, but in the case of your script, there is no sleep during the While statement without a block.

Explained: Block
You're right, the sleep is dropped without inner braces
But still reproducable, takes more time though (~2 minutes, without sleep the bug can be seen in 30 seconds)

btw can you reproduce it?

Code: Select all

#IfWinActive ahk_exe notepad.exe
LButton::
	While GetKeyState("LButton", "P") {
		Click
		Sleep, 80
	}
	Return
#IfWinActive

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: AHK has problem detecting LMB releasing

Post by mikeyww » 03 Jul 2022, 06:55

No. I held the mouse button for two minutes in Notepad, with no difficulty or "residual" clicking afterwards. You can increase the sleep until the problem disappears.

byzod
Posts: 87
Joined: 21 Jun 2021, 06:46

Re: AHK has problem detecting LMB releasing

Post by byzod » 03 Jul 2022, 07:50

mikeyww wrote:
03 Jul 2022, 06:55
No. I held the mouse button for two minutes in Notepad, with no difficulty or "residual" clicking afterwards. You can increase the sleep until the problem disappears.
Not hold all the way
4. Hold LMB on the button, release it after few seconds, repeat

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: AHK has problem detecting LMB releasing

Post by mikeyww » 03 Jul 2022, 09:35

I wonder if you are having a strange issue with your mouse, mouse broken, etc. You could try with a different mouse or computer. I have no additional ideas, but others here may.

Descolada
Posts: 1123
Joined: 23 Dec 2021, 02:30

Re: AHK has problem detecting LMB releasing

Post by Descolada » 03 Jul 2022, 10:57

I've had similar issues with LButton not being released or the LButton Up hotkey not always triggering. Using SetTimers has worked so far, so perhaps this would work:

Code: Select all

SetBatchLines, -1

#IfWinActive ahk_exe notepad.exe
LButton::
	Click
	SetTimer, ClickRoutine, 80
	While GetKeyState("LButton", "P") {
		Sleep, 10
	}
	SetTimer, ClickRoutine, Off
	Return
#IfWinActive

ClickRoutine:
	Click
	return

F5::ExitApp

Rohwedder
Posts: 7608
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: AHK has problem detecting LMB releasing

Post by Rohwedder » 03 Jul 2022, 11:41

Hallo,
use in Firefox your demo.html and try:

Code: Select all

SetBatchLines, -1
SetMouseDelay, -1
#InstallMouseHook
#IfWinActive ahk_exe Firefox.exe
LButton::
SetTimer, No, 100
Send, {F5} ;to reset demo.html
Sleep, 100
While, GetKeyState("LButton","P")
	Click,% (,No := A_Index)
SetTimer, No, -10
MsgBox, ***LMB Released***
Return
#IfWinActive
No:
ToolTip,% No
Return
While I clicked on "Click Me", Firefox only captured about half of the clicks. The message box appeared exactly when LButton was released. Firefox then continued counting as long as there were still clicks in the buffer.
AHK has no problems with LMB release detection, but Firefox with too large click rate.
I made a CPS.html (Clicks per Second) from your demo.html. My Firefox does not reach 400 CPS, Autohotkey exceeds 1000.
CPS.html:

Code: Select all

<!doctype html><head><title>Clicks per second</title><style>
.box{position:absolute;width:10em;}
.text{left:30%;top:5%;height:1em;overflow:hidden;
text-overflow:ellipsis;max-width:4em;}
.btn{left:30%;top:10%;width:4em;height:3em;}
.btn:active{background-color:red;}</style></head><body>
<button class="btn box" onclick=f()>Click Me</button>
<span class="text box">0</span>
<script>start=No=0;t = document.querySelector('.text');
function f(){if(!start)start=new Date; 
t.textContent=++No/(new Date-start)*1000}</script></body>

byzod
Posts: 87
Joined: 21 Jun 2021, 06:46

Re: AHK has problem detecting LMB releasing

Post by byzod » 04 Jul 2022, 04:43

Descolada wrote:
03 Jul 2022, 10:57
I've had similar issues with LButton not being released or the LButton Up hotkey not always triggering. Using SetTimers has worked so far, so perhaps this would work:

Code: Select all

SetBatchLines, -1

#IfWinActive ahk_exe notepad.exe
LButton::
	Click
	SetTimer, ClickRoutine, 80
	While GetKeyState("LButton", "P") {
		Sleep, 10
	}
	SetTimer, ClickRoutine, Off
	Return
#IfWinActive

ClickRoutine:
	Click
	return

F5::ExitApp
That's the alternative method I used, still stuck sometimes :(


Rohwedder wrote:
03 Jul 2022, 11:41
Hallo,
use in Firefox your demo.html and try:

Code: Select all

SetBatchLines, -1
SetMouseDelay, -1
#InstallMouseHook
#IfWinActive ahk_exe Firefox.exe
LButton::
SetTimer, No, 100
Send, {F5} ;to reset demo.html
Sleep, 100
While, GetKeyState("LButton","P")
	Click,% (,No := A_Index)
SetTimer, No, -10
MsgBox, ***LMB Released***
Return
#IfWinActive
No:
ToolTip,% No
Return
While I clicked on "Click Me", Firefox only captured about half of the clicks. The message box appeared exactly when LButton was released. Firefox then continued counting as long as there were still clicks in the buffer.
AHK has no problems with LMB release detection, but Firefox with too large click rate.
I made a CPS.html (Clicks per Second) from your demo.html. My Firefox does not reach 400 CPS, Autohotkey exceeds 1000.
CPS.html:

Code: Select all

<!doctype html><head><title>Clicks per second</title><style>
.box{position:absolute;width:10em;}
.text{left:30%;top:5%;height:1em;overflow:hidden;
text-overflow:ellipsis;max-width:4em;}
.btn{left:30%;top:10%;width:4em;height:3em;}
.btn:active{background-color:red;}</style></head><body>
<button class="btn box" onclick=f()>Click Me</button>
<span class="text box">0</span>
<script>start=No=0;t = document.querySelector('.text');
function f(){if(!start)start=new Date; 
t.textContent=++No/(new Date-start)*1000}</script></body>

Thanks for you test, then I found that
(1). The stuck problem not only happens to LMB, also applies to other button on mouse, I reproduced it with XButton1
(2). The stuck problem WON'T happen when bind key to keyboard button, for example "w" button (tested for 5 minutes)
(2.1) This difference might be related the different low-level signal handling of keyboard and mouse. When using mouse button as trigger, the trigger feels instantly, but with keyboard it feels like there's a internal delay for keypress
(3). I can't reproduce the "Firefox then continued counting as long as there were still clicks in the buffer" thing, my firefox count to 900 cps and stops when trigger key released. That might be related to cpu power or something, I do observe such "buffer click" thing in some web page games though
(4). As I said, to reproduce this bug, keep pressing LMB is not enough, you need to follow the step I wrote above:
4. Hold LMB on the button, release it after few seconds, repeat
I tweaked your code and tested it:
Demo for situation (1): stuck click after few tries, I move mouse to left edge when holding it to indicate the REAL physical state of mouse button press
An1.gif
An1.gif (821.03 KiB) Viewed 1278 times
Demo for (2): no stuck when using keyboard to trigger it, similar move method
An2.gif
An2.gif (384.45 KiB) Viewed 1278 times
Code for mouse demo:

Code: Select all

SetBatchLines, -1
SetMouseDelay, -1
SetKeyDelay, -1
#InstallMouseHook
#IfWinActive ahk_exe Firefox.exe
LButton::
SetTimer, No, 100
While, GetKeyState("LButton","P")
	Click,% (,No := A_Index)
SetTimer, No, off
ToolTip, %No% ***LMB Released***
Return
#IfWinActive
No:
ToolTip,% No
Return
Code for keyboard demo:

Code: Select all

SetBatchLines, -1
SetMouseDelay, -1
SetKeyDelay, -1
#InstallMouseHook
#IfWinActive ahk_exe Firefox.exe
w::
SetTimer, No, 100
While, GetKeyState("w","P")
	Click,% (,No := A_Index)
SetTimer, No, off
ToolTip, %No% ***w Released***
Return
#IfWinActive
No:
ToolTip,% No
Return
p.s. the count in second line is just to make sure firefox didn't missed any click, you can ignore it

Rohwedder
Posts: 7608
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: AHK has problem detecting LMB releasing

Post by Rohwedder » 04 Jul 2022, 10:17

Analysis of your gifs confirms your statement that your Firefox responds faster than your Autohotkey clicks and therefore does not need to use the buffer.
I can't see an Autohotkey stuck problem, since I don't know at what times you actually released the respective hotkey. I only see when Autohotkey detects this.
I assume that an online game reacts much slower than our HTML snippets. Could that be where your problem comes from?

byzod
Posts: 87
Joined: 21 Jun 2021, 06:46

Re: AHK has problem detecting LMB releasing

Post by byzod » 05 Jul 2022, 04:46

Hey guys! I located and solve the problem!

As you guys can't reproduce this bug so I test it on another pc with different mouse, no, I can't reproduce it on that machine

Then more tests and finally found that it's not related to hardware, it's because a line in my ahk

Code: Select all

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
which is inherited from the official built-in template...


With [sendmode input] and [SetMouseDelay, -1], the click stuck almost instantly
Deleted the line (the default send mode is event I suppose), and the click feels super extreme hyper responsive and completely no stuck happened :bravo:

This recommend is evil...!

Post Reply

Return to “Gaming Help (v1)”