How to use pixelsearch? - autohotkey

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

Related

Finding a pixel and tracking its color change

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}
}

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?

AutoHotkey: multiple repeating actions in a single script

I have 2 repeating actions that I want constantly waiting. Specifically, there are a couple of windows that, when they appear, I want AutoHotkey to perform an action on. For example, I want to move and resize the window with title "Window 1", and send a y keypress to "Window 2". So I have this:
win1.ahk
While, 1
{
WinWaitActive, Window 1
WinMove, Window 1, , 100, 100, 800, 600
}
win2.ahk
While, 1
{
WinWaitActive, Window 2
send y
}
My question is whether it is possible to put multiple such actions in the same AutoHotkey script. If I have 30 different windows that I want to wait on, do I need 30 individual .ahk scripts with this format, or is it possible to register several actions all in a single script?
No, just one script with a timer and a switch (a switch per action if you're sending keystrokes so they don't repeatedly send). Example, take out the notepadSentKey variable and see what happens when you open a new notepad.
#Persistent
SetTimer, DoStuff, 100
notepadSentKey := false
return
DoStuff:
{
IfWinActive, % "Untitled - Notepad"
{
WinMove, A,, 0, 0, 500, 200
if (!notepadSentKey) {
Send, Hello there{!}
notepadSentKey := true
}
}
IfWinActive, % "Calculator"
{
WinGetPos, x, y, w, h
WinMove, A,, % (A_ScreenWidth/2)-(w/2), % (A_ScreenHeight/2)-(h/2)
}
return
}

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}.

Can someone help me an AutoHotKey script?

I've read the help and guide for it but I just can't figure it out. All I want to do is create a hotkey that when pressed will move my mouse until the hotkey is pressed again. Can anyone tell me how to do this? It should be really simple but apparently I'm missing something.
This is pretty much the most annoying HotKey ever, but here you go (hotkey is Ctrl+Alt+C);
#MaxThreadsPerHotkey 3
^!c::
#MaxThreadsPerHotkey 1
if DoMouseMove
{
DoMouseMove := false
return
}
DoMouseMove := true
Loop
{
Sleep 100
Random, randX, 1, 1028
Random, randY, 1, 800
MouseMove, randX, randY, 25
Sleep, 100
if not DoMouseMove
break
}
DoMouseMove := false
return
I will throw in my solution.
pause::
If (Toggle := !Toggle) ; Toggle the value True/False
{
ToolTip Mouse Mover,A_ScreenWidth/2,0 ; Show that Mouse Mover is active
SetTimer MoveMouse, 1000 ; 1000 ms = 1 sec. Every minute (60000 ms) is probably enough.
}
Else
{
Tooltip ; Turn Mouse Mover alert window off
SetTimer MoveMouse, Off ; Turn the timer off
}
return
MoveMouse:
MouseMove, 1, 0, 1, R ;Move the mouse one pixel to the right
Sleep, 50 ; Wait 50 ms. Not realy required, but makes the move visible
MouseMove, -1, 0, 1, R ;Move the mouse back one pixel
return