Keywait + ErrorLevel Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Finallf
Posts: 18
Joined: 30 Oct 2020, 13:52

Keywait + ErrorLevel

Post by Finallf » 03 Jun 2021, 20:18

I'm porting a script to V2, and Keywait isn't working like V1.
I'm using the latest version: 2.0-a136-feda41f4
on V1 working:

Code: Select all

...
	~LButton::
		if GetKeyState("LButton") {
			Keywait, LButton, t.8
			if (ErrorLevel=1 and LClick=0) {
				RandomSleep(1,20) ;random sleep function
				SendInput, e
				LClick=1
			}
		}
on v2 it doesn't work

Code: Select all

...
	~LButton:: {
		if GetKeyState("LButton", "P") {
			Keywait "LButton", "T.8"
			if (Keywait=1 and LClick=0) {
				RandomSleep(1,20) ;random sleep function
				SendInput  "e"
				LClick=1
			}
		}
	}
When I keep the left mouse button pressed for 8 milliseconds it continues the script, if I press it for a shorter time it does not continue the second if
Instead of "ErrorLevel" I put it to check the Keywait function, as it says in the V2 documentation.
But I think I'm doing it wrong, because even with the mouse pressed for more than 8 milliseconds it doesn't pass the second if
If I remove "Keywait=1" and leave only the "LClick=0" check it passes, but doesn't wait for the 8 millisecond time, it passes as soon as it clicks.
Every help is welcome!

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Keywait + ErrorLevel

Post by swagfag » 03 Jun 2021, 20:37

  • uve used KeyWait() with command-like syntax, thereby discarding its return value
  • Keywait=1 compares the variable(which contains the implementation of the KeyWait() function. in v2 functions are variables) to the value 1. obviously a function does not compare equal to 1, so that would always return false and so would ur entire and-statement. in fact, due to short-circuit evaluation, its latter part is not even looked at
  • LClick=1 this is legacy/traditional assignment in v1(which u shouldnt have been using at all anyway), and a syntax error in v2. use the assignment operator := instead
  • LClick is neither a locally declared variable, nor is it a globally imported one, nor has it been captured, bound or passed in as a parameter. and assigning to a variable requires that one of these conditions is satisfied

Code: Select all

#Requires AutoHotkey v2.0-a136-feda41f4

LClick := 0

~LButton:: {
	if GetKeyState("LButton", "P") {
		global LClick
		if (Keywait("LButton", "T.8") and LClick = 0) {
			RandomSleep(1,20) ;random sleep function
			SendInput  "e"
			LClick := 1
		}
	}
}

Finallf
Posts: 18
Joined: 30 Oct 2020, 13:52

Re: Keywait + ErrorLevel

Post by Finallf » 03 Jun 2021, 21:11

Thanks for the quick response, I made the changes and it still has the same problem.
It does not respect the 8 millisecond time, it activates as soon as you click the mouse button.
I tested it with 2 seconds and even so it doesn't wait 2, it activates when it clicks.
I would like it to work like in V1:
When I press the left mouse button, it only activates after the stipulated time.

Code: Select all

...
Global LClick	:= 0
	~LButton:: {
		if GetKeyState("LButton") {
			if (Keywait("LButton", "T2") and LClick=0) {
				;RandomSleep(1,20)
				;SendInput {MButton}
				RandomSleep(1,20)
				SendInput "e"
				RandomSleep(1,20)
				SendInput "r"
				RandomSleep(1,20)
				SendInput "t"
				RandomSleep(1,20)
				SendInput "2"
				RandomSleep(1,20)
				SendInput "3"
				RandomSleep(1,20)
				SendInput "4"
				RandomSleep(1,20)
				SendInput "5"
				;SetTimer, Bmb, 7650
				SetTimer Be, 3130
				SetTimer Br, 27700
				SetTimer Bt, 10430
				SetTimer B2, 5050
				SetTimer B3, 5550
				SetTimer B4, 3850
				SetTimer B5, 6050
				Global LClick:=1
			}
		}
	}
...

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Keywait + ErrorLevel  Topic is solved

Post by swagfag » 04 Jun 2021, 04:01

v1 wrote:ErrorLevel is set to 1 if the command timed out or 0 otherwise.
v2 wrote:This function returns 0 (false) if the function timed out or 1 (true) otherwise.
presumably u need to swap ur logic around:

Code: Select all

if (!KeyWait(.....) and ....)
{
	...
}

Finallf
Posts: 18
Joined: 30 Oct 2020, 13:52

Re: Keywait + ErrorLevel

Post by Finallf » 04 Jun 2021, 14:48

Thanks for the reply, it worked, just had to remove the "D" option Keywait function, to work with the inversion of logic.
That's how it worked:

Code: Select all

...
	~LButton:: {
		if GetKeyState("LButton", "P") {
			if (!Keywait("LButton", "T1") and LClick=0) {
				;RandomSleep(1,20)
				;SendInput {MButton}
				RandomSleep(1,20)
				SendInput "e"
...				
			}
		}
	}		
...

Post Reply

Return to “Ask for Help (v2)”