help to fix this code Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

help to fix this code

Post by Green Astronaut » 23 Sep 2022, 13:55

I need help adapting my script.
I use the script below

Code: Select all

#SingleInstance, Force
CoordMode, Pixel, Screen

first_press := true       ; start your flag variable with a default value
pressed := false

while !pressed ; wait until the first time Printscreen is pressed before starting main loop
	Sleep, 100
pressed := false ; after it is made true by pressing Printscreen, turn it back to false

loop
{
	if (first_press)
	{
		while !pressed
		{
			PixelGetColor, Switch, 1209, 209, RGB
			if (Switch != 0x666055 && Switch = 0x2A2A2B)
				Send, {A}
			sleep, 200
		}
		pressed := false ; after it is made true by pressing Printscreen, turn it back to false 
		first_press := false
	}
	else
	{
		while !pressed
		{
			PixelGetColor, Switch, 1209, 209, RGB
			if (Switch != 0x2A2A2B && Switch = 0x666055)
				Send, {A} 
			sleep, 200
		}
		pressed := false ; after it is made true by pressing Printscreen, turn it back to false 
		first_press := true
	}
}
return

PrintScreen::pressed := true
when I press the Printscreen key to activate it starts searching for a color in a coordinate
and when I press to deactivate it looks for another certain color in the same coordinate.
use the "A" key in both cases, however I would like to use the "A" key to start instead, I would like to use the "A" key because I need to implement more searches and each search would be started with a specific key.



below I will leave the keys, the colors that will search when activated, the colors that will search when I want to deactivate and the coordinates


PS:
making everything work like a switch when turned on it would be in loop mode checking all the time, but when I press the same key to turn it off it would pause the script completely, only turning it on again when any of the defined keys were pressed again.


Keys: ------------------------------------- Colors -------------------------------------------------------------------------- Coordenates

(A) ------------------------------------- switched on : 0x666055 ----------- off : 0x7AAFAD -------------------------- x1233, y212,
(B) ------------------------------------- switched on : 0xB7972B ----------- off : 0xAF7A88 -------------------------- x1312, y218,
(C) ------------------------------------- switched on : 0xB79DA2 ----------- off : 0xC07DB9 ------------------------- x1428, y235,
(D) ------------------------------------- switched on : 0xDC16BB ----------- off : 0x7D7DC0 ------------------------- x1579, y247,
(E) ------------------------------------- switched on : 0x4416DC ----------- off : 0x7DC08F ------------------------- x1727, y259,
(F) ------------------------------------- switched on : 0x16DCD6 ----------- off : 0xC0A37D ------------------------- x1145, y261,


I really need help, if you have any doubts about my explanation, you can ask me and I will answer as soon as possible.

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

Re: help to fix this code

Post by mikeyww » 23 Sep 2022, 16:25

Below is perhaps a start in the right direction.

Code: Select all

#MaxThreadsPerHotkey 2

target := 0xFFFFFF ; Red-Green-Blue
x       = 1209     ; Coordinates relative to active window
y       = 209

PrintScreen::
MouseMove, x, y
SoundBeep, 1500
on := !on
While on {
 PixelGetColor, rgb, x, y, RGB
 If (rgb = target && on)
  Send x
 Sleep, 200 * on
}
Return
If you need concurrent searching, you might want to have separate timers for each routine. Alternatively, you could incorporate multiple searches into your single loop, according to which hotkeys had been activated.

Code: Select all

coord  := {a: [1233, 212, 0x666055]
         , b: [1312, 218, 0xB7972B]}

a::
b::
If Instr(search, A_ThisHotkey)
     search := StrReplace(search, A_ThisHotkey)
Else search .= A_ThisHotkey
If (search > "") {
 SetTimer, Search, 250
 SoundBeep, 1500
} Else {
 SetTimer, Search, Off
 SoundBeep, 1000
}
Return

Search:
SoundBeep, 1900
Loop, Parse, search
{ MouseMove, coord[A_LoopField].1, coord[A_LoopField].2
  PixelGetColor, rgb, coord[A_LoopField].1, coord[A_LoopField].2, RGB
  If (rgb = coord[A_LoopField].3)
   Send x
}
Return

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help to fix this code

Post by Green Astronaut » 23 Sep 2022, 18:22

@mikeyww working perfectly.

this code worked perfectly, the mouse moves to the specific coordinate and I hear the beeps
I just need two adjustments

the first : when the key is "A" is pressed instead of moving the mouse to the coordinate, send the key press "A" because in the program I am using the "A" makes a function so when I call with the "A" key, it will send "A" repeated until it is turned off

the second : implement up to the letter F, because each letter will work in the same way as "A" by sending the keys.

it's possible ?

I used the code below:

Code: Select all

coord  := {a: [1233, 212, 0x666055]
         , b: [1312, 218, 0xB7972B]}

a::
b::
If Instr(search, A_ThisHotkey)
     search := StrReplace(search, A_ThisHotkey)
Else search .= A_ThisHotkey
If (search > "") {
 SetTimer, Search, 250
 SoundBeep, 1500
} Else {
 SetTimer, Search, Off
 SoundBeep, 1000
}
Return

Search:
SoundBeep, 1900
Loop, Parse, search
{ MouseMove, coord[A_LoopField].1, coord[A_LoopField].2
  PixelGetColor, rgb, coord[A_LoopField].1, coord[A_LoopField].2, RGB
  If (rgb = coord[A_LoopField].3)
   Send x
}
Return

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

Re: help to fix this code

Post by mikeyww » 24 Sep 2022, 06:51

This is just a demonstration. Adjust to meet your needs. You can delete any commands that you do not need.

Code: Select all

coord  := {a: [1233, 212, 0x666055]
         , b: [1312, 218, 0xB7972B]
         , c: [1428, 235, 0xB79DA2]
         , d: [1579, 247, 0xDC16BB]
         , e: [1727, 259, 0x4416DC]
         , f: [1145, 261, 0x16DCD6]}

#UseHook
a::
b::
c::
d::
e::
f::
If Instr(search, A_ThisHotkey)
     search := StrReplace(search, A_ThisHotkey)
Else search .= A_ThisHotkey
If (search > "") {
 SetTimer, Search, 250
 SoundBeep, 1500
} Else {
 SetTimer, Search, Off
 SoundBeep, 1000
}
Return
#UseHook Off

Search:
SoundBeep, 1900
Loop, Parse, search
{ MouseMove, coord[A_LoopField].1, coord[A_LoopField].2
  Send %A_LoopField%
  PixelGetColor, rgb, coord[A_LoopField].1, coord[A_LoopField].2, RGB
  If (rgb = coord[A_LoopField].3)
   Send x
}
Return

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help to fix this code

Post by Green Astronaut » 24 Sep 2022, 12:09

@mikeyww
I forgot to ask you to adjust one thing:

it is working as i need.

however when I turn on any of the keys, I want them to start being pressed automatically only if the color in the coordinates is not found.

example:
I turned on the "A" key, check if the color 0x666055 is found at the coordinate 1233, 212 and press the "A" key again only if it is not found!
will only be pressed if each of the pixels is not found, if the pixel in the coordinate is found there will be no action

Can you help me with this final adjustment?

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

Re: help to fix this code

Post by mikeyww » 25 Sep 2022, 06:30

It's time to learn some AutoHotkey. You currently have

Code: Select all

rgb = coord[A_LoopField].3
Instead of = for "equals", you can use != for "not equals".

Explained: Not equal

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help to fix this code

Post by Green Astronaut » 25 Sep 2022, 08:44

@mikeyww
I understand, the only problem is that when I press "A" to turn it on, it keeps spamming the A key even though it found the color in the coordinate.

I wanted that when the color is found, the "A" key would no longer be sent, but even finding the color, it keeps sending the "A" key

this is the only problem i have identified


It is possible to do something like this:

if you don't find it press "A" if you find it don't do anything and continue the code

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

Re: help to fix this code  Topic is solved

Post by mikeyww » 25 Sep 2022, 08:59

You might want to read documentation about If? :think:

Code: Select all

coord  := {a: [1233, 212, 0x666055]
         , b: [1312, 218, 0xB7972B]
         , c: [1428, 235, 0xB79DA2]
         , d: [1579, 247, 0xDC16BB]
         , e: [1727, 259, 0x4416DC]
         , f: [1145, 261, 0x16DCD6]}

#UseHook
a::
b::
c::
d::
e::
f::
If Instr(search, A_ThisHotkey)
     search := StrReplace(search, A_ThisHotkey)
Else search .= A_ThisHotkey
If (search > "") {
 SetTimer, Search, 250
 SoundBeep, 1500
} Else {
 SetTimer, Search, Off
 SoundBeep, 1000
}
Return
#UseHook Off

Search:
SoundBeep, 1900
Loop, Parse, search
{ PixelGetColor, rgb, coord[A_LoopField].1, coord[A_LoopField].2, RGB
  ToolTip, %rgb%
  If (rgb = coord[A_LoopField].3) { ; Color was found
   Send x
  } Else {                          ; Color was not found
   Send %A_LoopField%
  }
}
Return

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help to fix this code

Post by Green Astronaut » 25 Sep 2022, 09:41

@mikeyww

working perfectly.

Thanks for the explanations and for your time!
I have no words to thank you for your help!

Post Reply

Return to “Ask for Help (v1)”