Keys are pressed slowly at PixelGetColor Topic is solved

Ask gaming related questions (AHK v1.1 and older)
narvell3
Posts: 7
Joined: 13 Feb 2019, 02:59

Keys are pressed slowly at PixelGetColor

Post by narvell3 » 21 Mar 2023, 11:15

Hello. I have recently noticed that clicking the script has become slower with PixelGetColor. Or rather, when the script itself grew a little in length, added more button values, then there was a delay. It happens that the script works instantly and presses the buttons quickly, and sometimes there is a delay in the form that the pixel has already appeared and the creak still thinks somewhere in the order of 2-4ms. I checked with my hands to press the buttons when a pixel appears, when pressing the keys everything works quickly without delay, but the script 50/50 sometimes presses quickly and sometimes something slows down. The script itself is below. I would be very grateful for help in optimization. P.S. There is a feeling, so to speak, that because of the many pixels and processing, the script slows down its work.

Code: Select all

SetKeyDelay, -1, -1
SetMouseDelay, -1
SetBatchLines, -1
vk45::
Loop
{
GetKeyState, state, vk45
if state = U
break

PixelGetColor, color, 622, 670, RGB 
If (color = 0x566272)
{
Send, {1}
}

PixelGetColor, color, 625, 665, RGB 
If (color = 0xD8FAFF)
{
Send, {1}
}

PixelGetColor, color, 623, 659, RGB 
If (color = 0xE0741D)
{
Send, {2}
}

PixelGetColor, color, 626, 667, RGB 
If (color = 0xFED237)
{
Send, {3}
}

PixelGetColor, color, 628, 667, RGB 
If (color = 0x006FB9)
{
Send, {4}
}

PixelGetColor, color, 626, 670, RGB 
If (color = 0xD53617)
{
Send, {5}
}

PixelGetColor, color, 624, 666, RGB 
If (color = 0x085ED9)
{
Send, {6}
}

PixelGetColor, color, 628, 664, RGB 
If (color = 0x7D83AE)
{
Send, ^{1}
}

PixelGetColor, color, 623, 665, RGB 
If (color = 0x00223E)
{
Send, +{q}
}

PixelGetColor, color, 625, 664, RGB 
If (color = 0xECE4FB)
{
Send, +{2}
}

PixelGetColor, color, 627, 668, RGB 
If (color = 0xFFFF67)
{
Send, +{r}
}

PixelGetColor, color, 624, 663, RGB 
If (color = 0x1259BE)
{
Send, {g}
}

PixelGetColor, color, 626, 674, RGB 
If (color = 0x0B4A9A)
{
Send, {f}
}

PixelGetColor, color, 627, 669, RGB 
If (color = 0xFFFE45)
{
Send, {h}
}

PixelGetColor, color, 623, 666, RGB 
If (color = 0x5CD5F8)
{
Send, {t}
}

PixelGetColor, color, 631, 672, RGB 
If (color = 0x7757B1)
{
Send, {5}
}

PixelGetColor, color, 626, 668, RGB 
If (color = 0xA03000)
{
Send, {y}
}

PixelGetColor, color, 624, 668, RGB 
If (color = 0x213536)
{
Send, {q}
}

PixelGetColor, color, 625, 662, RGB 
If (color = 0xA1E8F3)
{
Send, {=}
}

PixelGetColor, color, 625, 665, RGB 
If (color = 0x777A8E)
{
Send, {-}
}

PixelGetColor, color, 627, 664, RGB 
If (color = 0xFE9B19)
{
Send, {0}
}

PixelGetColor, color, 624, 674, RGB 
If (color = 0x8D77EB)
{
Send, {f2}
}

PixelGetColor, color, 624, 669, RGB 
If (color = 0xC77F43)
{
Send, {x}
}

}
Return

End::
  ExitApp
Return

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

Re: Keys are pressed slowly at PixelGetColor

Post by mikeyww » 21 Mar 2023, 19:54

You're right. When you add more pixel checks, the routine takes longer to run. You can compute the time yourself.

Code: Select all

#Requires AutoHotkey v1.1.33
start := A_TickCount
PixelGetColor, color, 622, 670, RGB 
PixelGetColor, color, 625, 665, RGB 
PixelGetColor, color, 623, 659, RGB 
PixelGetColor, color, 626, 667, RGB 
PixelGetColor, color, 628, 667, RGB 
PixelGetColor, color, 626, 670, RGB 
PixelGetColor, color, 624, 666, RGB 
PixelGetColor, color, 628, 664, RGB 
PixelGetColor, color, 623, 665, RGB 
PixelGetColor, color, 625, 664, RGB 
PixelGetColor, color, 627, 668, RGB 
PixelGetColor, color, 624, 663, RGB 
PixelGetColor, color, 626, 674, RGB 
PixelGetColor, color, 627, 669, RGB 
PixelGetColor, color, 623, 666, RGB 
PixelGetColor, color, 631, 672, RGB 
PixelGetColor, color, 626, 668, RGB 
PixelGetColor, color, 624, 668, RGB 
PixelGetColor, color, 625, 662, RGB 
PixelGetColor, color, 625, 665, RGB 
PixelGetColor, color, 627, 664, RGB 
PixelGetColor, color, 624, 674, RGB 
PixelGetColor, color, 624, 669, RGB 
MsgBox 64, Elapsed time, % A_TickCount - start " ms"
image230321-2054-001.png
Output
image230321-2054-001.png (4.42 KiB) Viewed 673 times

narvell3
Posts: 7
Joined: 13 Feb 2019, 02:59

Re: Keys are pressed slowly at PixelGetColor

Post by narvell3 » 22 Mar 2023, 08:49

@mikeyww
And please tell me how to do it better then? I'm still not good at AHK.

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

Re: Keys are pressed slowly at PixelGetColor

Post by mikeyww » 22 Mar 2023, 09:57

I do not have a way to make the pixel commands execute more quickly. Others may have some fancier ideas here-- screen grab, analyze the bitmap, etc.

narvell3
Posts: 7
Joined: 13 Feb 2019, 02:59

Re: Keys are pressed slowly at PixelGetColor

Post by narvell3 » 22 Mar 2023, 11:42

@mikeyww
Thanks anyway for pinpointing the problem. I hope someone can help. Thanks again for the help.

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

Re: Keys are pressed slowly at PixelGetColor

Post by mikeyww » 22 Mar 2023, 11:48

Can also set batch lines to -1. You could save a bunch of milliseconds there.

narvell3
Posts: 7
Joined: 13 Feb 2019, 02:59

Re: Keys are pressed slowly at PixelGetColor

Post by narvell3 » 02 Apr 2023, 14:14

@mikeyww
Hello. I haven't optimized the script much through masiva. It works very fast. But not cyclically. I always have to press the E key on the keyboard to trigger the script. Previously, it was just enough for me to hold down the E key and the script cyclically repeated the actions in order without stopping. Plus, now for some reason the script cannot work at the same time when I hold down the E key and let's say I press the WASD key, it does not respond at this moment. It only works when I press one key. In a place with other keys, it does not work, Can you tell me where I made a mistake? thank you in advance for your help.

Code: Select all

#NoEnv
SetBatchLines, -1
SetWorkingDir %A_ScriptDir%
SendMode Input
CoordMode, Pixel, Window

posX := [627,629,629,626,630,631,625,626]
posY := [664,664,663,662,669,672,664,662]
clr := ["0x413016","0xE0A85F","0x1E163E","0x887A7E","0x4B9300","0xA24099","0x790916","0x542502"]
char := ["2","3","t","5","1","4","g","f"]

vk45::
Loop
{
		PixelGetColor, color, posX[A_Index], posY[A_Index], RGB
	    	if (color = clr[A_Index])
		{
		    Send % char[A_Index]	
		}
}
return
End::
  ExitApp
Return

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

Re: Keys are pressed slowly at PixelGetColor

Post by mikeyww » 02 Apr 2023, 14:29

1. What is your belief about the value of posX[A_Index] during the loop's 20th iteration?

2. If you add a line to the script to display that value, does it confirm your belief?

narvell3
Posts: 7
Joined: 13 Feb 2019, 02:59

Re: Keys are pressed slowly at PixelGetColor

Post by narvell3 » 02 Apr 2023, 16:43

Thanks for the tip. I added Loop 11 to it and now the code works cyclically, processing all clicks. But the second problem remained. If I hold down a key and want to press other keys at this moment, as I indicated above in the WASD example, then the codd stops working. I have to repeatedly press and hold the key and not press any of the keys on the keyboard at this moment. Because at the moment of the other keys, while I hold the E key, the script does not allow me to work and press others at the same time. Can you tell me how to solve this problem ?

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

Re: Keys are pressed slowly at PixelGetColor  Topic is solved

Post by mikeyww » 02 Apr 2023, 18:10

Code: Select all

#Requires AutoHotkey v1.1.33
pos := [[627, 664, 0x413016,  2 ]
      , [629, 664, 0xE0A85F,  3 ]
      , [629, 663, 0x1E163E, "t"]
      , [626, 662, 0x887A7E,  5 ]
      , [630, 669, 0x4B9300,  1 ]
      , [631, 672, 0xA24099,  4 ]
      , [625, 664, 0x790916, "g"]
      , [626, 662, 0x542502, "f"]]

e::
SoundBeep 1500
While GetKeyState("e", "P")
 For each, set in pos {
  PixelGetColor rgb, set[1], set[2], RGB
  Send % rgb = set[3] ? set[4] : ""
 } Until !GetKeyState("e", "P")
SoundBeep 1000
Return

narvell3
Posts: 7
Joined: 13 Feb 2019, 02:59

Re: Keys are pressed slowly at PixelGetColor

Post by narvell3 » 03 Apr 2023, 11:45

@mikeyww
You're just a wizard. I haven't been so happy in the game for a long time. Thank you for your time. Thank you very much. You're the best.

Post Reply

Return to “Gaming Help (v1)”