Finding a pixel and tracking its color change - autohotkey

There is a script that for some reason does not work.
I explain the situation: There is a mini game in which there are green rings and they change their position (not random) every time the white ring stops using the space bar on them.
I wrote a little script to find the green color, but for some reason it does not work.
F5::
loop {
PixelGetColor, color1, 957, 672, RGB
PixelGetColor, color2, 957, 672, RGB
if(color1 != 0x10A04B){
Sleep, 80
} else {
Soundbeep
Sleep, 80
}
if(color2 != 0xFDFFFE){
Sleep, 80
} else {
Send, {space}
}
}
return
To help you fully understand the mini game, I'm sending you a link to the video: https://youtu.be/b4y1aiQNea4
Please help me understand the implementation. Thank you!

Use a timer instead of the loop
Try using Alt or Slow mode
For finding the green ring coordinates try using PixelSearch, then you will be able to specify variation of the color (it's necessary due to it being partially transparent)

colorchange(x,y){
PixelGetColor,color,x,y
if (color=0xffffffffff){
Return 1}Else{
Return 0}
}
if colorchange(X,Y){
Send,{Space}
}

Related

How to use pixelsearch?

I have been trying to put together this script that clicks on the pixel with the specified color.
It keeps crashing and clicking unrelated spots.
Is there a better way to implement this? This is what I have (sorry i'm very new to this)
Loop {
PixelSearch, OutputVarX, OutputVarY, 400, 300, 1800, 800, 821A83, Fast
if (ErrorLevel){
MsgBox, no
}
else if (!ErrorLevel)
{
Click, %OutputVarX%, %OutputVarY% Down
Sleep 2000
Click, Up
}
} Until, limit > 0
limit = 0
^w::
limit += 1
return
I answered a similar post a little while ago that might help you (AHK) Can't seem to get a pixel color statement working. Basically using a Timer instead of a Loop would probably be more helpful to you. I edited your code below to what I think might work, but still believe the Timer is a better solution (command SetTimer).
CoordMode, Mouse, Screen ; Or 'Relative' as you desire
CoordMode, Pixel, Screen
; this tells the the pixel search to be relative to the screen
; and the mouse click as well. that might be what is missing here.
Loop {
PixelSearch, OutputVarX, OutputVarY, 400, 300, 1800, 800, 0x821A83, Fast ; decimal or hexadecimal (including "0x" prefix)
if (ErrorLevel) {
MsgBox, no
}
else if (!ErrorLevel)
{
Click, %OutputVarX%, %OutputVarY%, Down
Sleep 2000
Click, Up
}
} Until (limit > 0)
limit = 0
Return
; each block should have a Return unless you want it
; to drop into the code below
^w::
limit += 1
return

AHK Script with ImageSearch

I wrote script that automatically detects play button on the screen and clicks it.
Here it is:
SetTimer Go, 1000
CoordMode Pixel, Screen
CoordMode Mouse, Screen
^!r:: Reload
F4::T4 := !T4
F5::play()
Go:
If (!T4){
return
}
play()
return
play(){
FoundX := 0
FoundY := 0
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, play2.png
If (ErrorLevel = 2){
MsgBox Could not conduct the search.
}
Else{
If (ErrorLevel = 1){
return
}
Else{
x := FoundX + 40
y := FoundY + 40
MouseClick, left, x, y
}
}
}
While in regular window it works fine in fullscreen (fullscreened borderless window) it's behaving weird.
For example sometimes when it sees the button it clicks but then keeps clicking even thou its not on the screen anymore. What's more even if i reload the script it would still keep clicking in that spot. After pressing play a fast-forward button which is fairly similar. Does ImageSearch have some tolerance settings?
The other sorcery is that when i change focus to another window (which is on top but the play button is still visible) it clicks, which change the focus back, and will not click anymore even after the button is back. However if use ALT+TAB to go back to that other window it triggers.
Can anyone explain to me wth is going on here?

Click and drag one pixel in given direction

I am looking for a script that will click and hold the right mouse button down and drag in a given direction 1 pixel every "x" or 2 seconds. Something I can either tell to move in given direction by hitting the corresponding direction key or by adjusting the script manually.
Thank you!
Not entirely sure what you want, but try this:
CoordMode, Pixel, Screen
direction = left
secondsBetweenMoves = 0.1
F1:: ;F1 to start it
SendInput, {LButton Down}
SetTimer, Move, %secondsBetweenMoves%
Return
F2:: ;F2 to end it
SendInput, {LButton Up}
SetTimer, Move, Off
Return
Move:
MouseGetPos, mouseX, mouseY
If (direction = "left") {
MouseMove, mouseX-1, mouseY
}
Else If (direction = "right") {
MouseMove, mouseX+1, mouseY
}
Else If (direction = "up") {
MouseMove, mouseX, mouseY-1
}
Else If (direction = "down") {
MouseMove, mouseX, mouseY+1
}
Return
The send event command might make your life a whole lot easier. For example, what you're trying to do in a simplistic infinite loop:
coordmode, mouse, screen
setmousedelay, 0 # This makes the mouse move extremely fast
loop {
mousegetpos, mx, my
mx := mx+1
sendevent {click, r, down}
sendevent {click, %mx%, %my%, r, up}
sleep 2000
}
This loop will hold down the right button and move one pixel to the right every two seconds. If you want it to move to the left, change
mx := mx+1
to
mx := mx-1
Making it move up or down is the same mechanism, but adding or subtracting to the variable "my" instead.
This loop can be made into a timer, with hotkeys to enable or disable it.

pixelsearch and click right pixel

IF NOT A_IsAdmin ; Runs script as Admin.
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}
#MaxThreadsPerHotkey, 2
CoordMode, Pixel, Screen
#singleInstance, Force
toggle = 0
upperLeftX := 750
upperLeftY := 400
lowerRightX := 850
lowerRightY := 500
F8:: ; press F8 to toggle the loop on/off.
SoundBeep
Toggle := !Toggle
While Toggle
{ ;-- Begin of loop.
PixelSearch, X, Y,%upperLeftX%, %upperLeftY%, %lowerRightX%, %lowerRightY%, 0x000000, 0, Fast RGB
IF ErrorLevel = 1 ; IF NOTFound.
{
sleep, 100
}
IF ErrorLevel = 0 ; IF Found.
{
MouseClick, left
sleep, 300
}
} ;-- End of Loop.
return
F8 starts loop and this code checks specific pixel in rectangle and sends left click.
It works with [MouseClick, left, %X%, %Y%].But I want to know how can I use dllcall mouse event to click on specific pixel.
for example
DllCall("mouse_event",uint,1,int,%X%,int,%Y%,uint,0,int,0)
But its not working
I doubt that you actually want to do this via DLL calls. mouse_event doesn't even take coordinates, but values between 0-65535.
If you want to be able to click any pixel on the screen make sure you set it to be relative to the screen: CoordMode, Mouse, Screen
Then use ControlClick/PostMessage/SendMessage if you don't want to affect your mouse pointer by that click. Or use MouseClick/Click. Or MouseMove+Send, {LButton}.

AutoHotKey - ImageSearch Two Images

Is there any way to have the ImageSearch command on AutoHotKey look for two images
Currently my code is this:
Loop
{
ImageSearch, wherexis, whereyis, 664, 556, 1364, 924, C:\Users\AHK Stuff\continue.PNG
If ! ErrorLevel
{
Click %wherexis%,%whereyis%
break
}
}
Sleep, 1400
Sometimes the screen displays on image and other times it displays another so oftentimes my code stops running since it cannot find the image. How can I make an "or" kind of statement to make the ImageSearch look for two images and click whatever shows up?
Thanks!
Just do another imagesearch in case the first one wasn't found:
Loop
{
ImageSearch, wherexis, whereyis, 664, 556, 1364, 924, C:\Users\AHK Stuff\continue.PNG
If ! ErrorLevel
{ ;If the first image was found:
Click %wherexis%,%whereyis%
break
}
Else
{ ;If the first image was not found:
ImageSearch, wherexis, whereyis, 664, 556, 1364, 924, C:\Users\AHK Stuff\image2.PNG
If ! ErrorLevel
{ ;If the second image was found:
Click %wherexis%,%whereyis%
break
}
}
Sleep, 100
}
Sleep, 1400