Page 1 of 1

Click doesn't work with variables

Posted: 20 Feb 2020, 10:20
by timg11
This code works:

Code: Select all

Click 300,575
This code does not work. The click never happens. I have logged PsX and PsY to a file, and those variables contain the correct values.

Code: Select all

PsX := GPwidth // 2                  ; middle from left
PsY := ( GPheight -  17)   ; from bottom
Click %PsX%, %PsY%
FileAppend, %A_YYYY%-%A_MM%-%A_DD% -------------- Click X:%PsX% Y:%PsY%`n, C:\data\ahk-log.txt
I am basing my syntax on the help page for click where it says: "Click, %x%, %y% Since click does not support expressions, variables should be enclosed in percent signs."

I also tried it this way:

Code: Select all

PsX := GPwidth // 2                  ; middle from left
PsY := ( GPheight -  17)   ; from bottom
MouseMove PsX, PsY
Click 
FileAppend, %A_YYYY%-%A_MM%-%A_DD% -------------- Click X:%PsX% Y:%PsY%`n, C:\data\ahk-log.txt
In this case I did not use the % around variables since the help for MouseMove says "The x/y coordinates to move the mouse to, which can be expressions"
Still, the mouse does not move, and the click does not happen.


What did I miss?

Re: Click doesn't work with variables

Posted: 20 Feb 2020, 11:28
by boiler
timg11 wrote:
20 Feb 2020, 10:20
I have logged PsX and PsY to a file, and those variables contain the correct values.
Do you mean that you logged GPwidth and GPheight to a file? Because you're overwriting PsX and PsY with new values in the lines below. If you read values into PsX and PsY from a file, they got wiped out by the lines below. Where do you assign GPwidth and GPheight?

Code: Select all

PsX := GPwidth // 2                  ; middle from left
PsY := ( GPheight -  17)   ; from bottom

Re: Click doesn't work with variables

Posted: 21 Feb 2020, 09:01
by timg11
Note: The code fragments above that set values for PxX and PXy are preceded by this statement that sets GPwidth and GPheight

Code: Select all

 WinGetPos, Xpos, Ypos, GPwidth, GPheight, WindowTitle

Re: Click doesn't work with variables

Posted: 21 Feb 2020, 09:59
by boiler
If you're saying that PsX and PsY are shown to be 300 and 575 respectively, and you replace Click %PsX%, %PsY% with Click 300, 575 respectively into otherwise the exact same code and the latter works and the former doesn't, I don't know what to tell you. It would seem that there is some other difference in your test when you click with 300 and 575.

I don't see how the issue could be that Click isn't working with the variables. It either isn't working with numbers in the identical scenario or the variables don't contain the same values as the numbers. Sometimes when debugging, you get to the point where you swear that somehow the language is screwing up because you've totally isolated everything else, but the issue with the code is eventually found.

Rather than a log which you check after the fact, you might try replacing that click line with the following lines so you can see the values in real time, and compare the click using variables with the click using numbers.

Code: Select all

SplashTextOn, 200, 50, Click with variables, About to click %PsX%, %PsY%
Sleep, 3000
SplashTextOff
Sleep, 200
Click, %PsX%, %PsY%
Sleep, 1000
SplashTextOn, 200, 50, Click with numbers, About to click 300, 575
Sleep, 3000
SplashTextOff
Sleep, 200
Click, 300, 575

Re: Click doesn't work with variables

Posted: 22 Feb 2020, 11:37
by timg11
Here's a larger block of the code:

Code: Select all

        ;    	Click 300,575
                loop, 5
                {
                	PixelGetColor, Pxcolor, %PsX%, %PsY%
                        FileAppend, %A_YYYY%-%A_MM%-%A_DD% -------- Color:%Pxcolor% X:%PsX% Y:%PsY%`n, C:\data\ahk-log.txt
                	if ( Pxcolor < 0xFFFFFF) 
                	{
                		MouseMove PsX, PsY
                		Click 
                                FileAppend, %A_YYYY%-%A_MM%-%A_DD% -------------- Clicked X:%PsX% Y:%PsY%`n, C:\data\ahk-log.txt
                		break
                	}
                	PsY := ( PsY -  30) 
              	}
Currently the numeric value click is commented out, and the iterative search is uncommented.
Basically, I'm trying to click a button. For unknown reasons the button sometimes moves vertically.
The Click 300,575 position is where it is located usually, but not always.
I tried ImageSearch and ImageSearchC to locate the button, but could never get them to work.
My current approach is to loop and move upward by steps of 30 pixels until the button color is found by the PixelGetColor, then click that spot.
In this loop, the mouse does not move to the coordinates, the click has no effect, yet the two FileAppends correctly log the color and coordinates.

I'll try your debugging approach when I have a chance to work on it some more.
Thanks!

Re: Click doesn't work with variables

Posted: 22 Feb 2020, 14:00
by boiler
If the numeric click works, then this probably isn't the problem, but are you sure that CoordMode for both Pixel and Mouse are set the same?