AutoHotkey ImageSearch function not working - macros

how are you?
I'm using AHK v1.1.33.06 and I'm trying to execute a simple script which tries to find an image on whole screen, but it never works, I can see the image on my screen but the code can't haha.
I also tried to look for a part of the complete image, but I get the same error...
This is my code:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance, Force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
CoordMode Pixel
WinActivate, Kakele
while 1 = 1
{
sleep, 5000
;ImageSearch, OutputVarX, OutputVarY, 0, 0, 711, 343, Reflect.bmp
ImageSearch,x,y,0,0,%A_ScreenWidth%,%A_ScreenHeight%, *w12 *h12 Reflect.JPG
if (ErrorLevel = 2)
MsgBox Could not conduct the search.
else if (ErrorLevel = 1)
MsgBox Icon could not be found on the screen.
else
MsgBox The icon was found at %x%x%y%.
;sleep, 100
}
return
I tried with jpg, png and bmp formats but my code never finds the imgage. I tried *w12 and *h12 because this is the image fragment resolution.
Please see the images I'm trying to look for, the first one is the whole game complete screen, the second one the fragment of the icon on upper left region, the double sword one.
Is there any hint or something I'm doing wrong? I'm going crazy because os this.

Related

How to click in two different programs at the same time?

I want to be able to start a timer at the same time that i start a video in media player classic & also be able to pause both at the same time.
lets say that i have to click in the timer the one tha tis in the top left of the image and then click f1 or else the script does not work... also if i want to click it again after the first time it does not work properly..
I tried the following code:
F1::
MouseClick, left, 146, 115
MouseClick, left, 87, 300
Return
I suppose that adding
CoordMode, Mouse, Screen
at the top of your script will help you achieve what you're looking to do.
However, I suggest you use ControlClick instead.
F1::
ControlClick, x87 y300, Hourglass,, Left
ControlClick, x100 y100, Media Player Classic,, Left
return
This will send a click to the x and y relative to each window regardless if they're on top or not (they cannot, however, be minimized)
(please note that you probably have to adjust the x and y coordinates)
https://autohotkey.com/docs/commands/CoordMode.htm
https://autohotkey.com/docs/commands/ControlClick.htm
From my (limited) experience, I believe even hotkeys will carry out actions sequentially i.e. one after the other, in most cases the time delay between clicks will be insignificant howeverwon't be exact.
If the timer (Hourglass) is something you have created yourself, I would maybe suggest adding another button or a hotkey specifically for this use case that adds; for example, a 0.5s delay before the timer begins & then starts the media 0.5s later allowing for them to work in tandem, but as far as starting both applications simultaneously I'm not aware of a method for this.
To clarify though, what was the actual issue with the quoted code above?

ImageSearch command failing

I'm trying to create a hotkey that will find the google sheets "tools" menu, click on it, move down a few pixels and click on script editor. I could do this within google sheets but it doesn't have universal keybinds for this, I'd have to make a macro and I'd rather it just be universal.
SC163::
{
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\xx\Documents\AHK Scripts\gsheets-tools.bmp
if ErrorLevel = 2
tooltip Could not conduct the search.
else if ErrorLevel = 1
tooltip Image could not be found on the screen.
else
{
mousemove, %FoundX%, %FoundY%, 50
tooltip The image was found at %FoundX%x%FoundY%.
}
return
}
It was throwing error 2 until I hardcoded the entire image name. Now it only throws error 1, even if I have three copies of sheets open (one on each monitor). Is BMP not the best format to use here? I tried using
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *n30 C:\Users\xx\Documents\AHK Scripts\gsheets-tools.bmp
to see if adding that allowance would helps, but failure. Any tips for using image search correctly? This isn't some sophisticated game function, just trying to find a toolbar and click on it!
Try changing *n30 to just *30.
Also, by default, ImageSearch coordinates are per the active window and not the entire desktop, so using A_ScreenWidth and A_SreeenHeight might be problematic. While this isn't an issue if the window is maximized on your primary screen, it may present a problem if it's not maximized or is on a screen with a different resolution. If it's not maximized, it could be searching an area that goes beyond your display area, which might be an issue.
Possibly another issue (unverified) could be with Windows scaling. A lot of times a 2160p monitor defaults to 200% scaling (or is it 150%? w/e).
Since you're using BMP images, you may want to verify that it was saved as 16-bit or higher.
Notable quotes from the help file:
All operating systems support GIF, JPG, BMP, ICO, CUR, and ANI images (BMP images must be 16-bit or higher).
. . .
The region to be searched must be visible; in other words, it is not possible to search a region of a window hidden behind another window. By contrast, images that lie partially beneath the mouse cursor can usually be detected. The exception to this is game cursors, which in most cases will obstruct any images beneath them.
I suggest you using this for Image Search
https://www.youtube.com/watch?v=aWRAtvJq9ZE
It is very simple and works pretty good!

AutoHotkey's MouseClickDrag issues in dealing with selecting Paint.NET images squared area?

(If you want to reproduce this example, please download Paint.NET from here, it's free and it's got more functions than Microsoft Paint).
When you open Paint.NET, load a simple image and take rectangular selection as instrument.
I've written this simple script:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
^d::
MouseClickDrag, Left, 365, 434, 906, 460
;You can change mouse coordinates as you wish
Send, ^c
Return
Run it with Ctrl + d.
I'm not able to select the area described by those coordinates, while using the rectangular color filling instrument works fine.
It seems AutoHotkey does not highlight the area if I use the rectangular selector.
Have you got the same issue?
If SendMode Input doesn't work, Event usually does. Unfortunately, it's not as fast as Input, so playing around with SetKeyDelay may help going against that effect.
However, if you're looking for a way to manipulate images automatically, I recommend using GDI+.

Autohotkey pixelsearch including mouse

I have done a decent amount of researching and I apologize in advance if I have missed the answer to this question in the documentation or forums somewhere.
I would like to do a pixel search of several area's and have a script react to the area when my mouse moves over that area, my idea was to use a custom cursor with a pixel shade not found in the application I'm manipulating, but Pixelsearch nor PixelgetColor seem to recognize the mouse, am I doing something wrong or did I miss something, or is this simply not possible?
Loop
{
PixelSearch, pxcolor, pxcolor1, 872, 367, 893, 394, 0x5F415F, 3, Fast
sleep, 200
if pxcolor < 0
click left
sleep, 200
}
So basically, the script is doing a constant check for a pixel of that shade in that area, and when my mouse moves over that area (the cursor being what contains the pixel shade), the script reacts.
What happens instead is it only sees the pixels of the window, and takes no notice of my cursor.
Trie something like this out, may do what you need
#Persistent
SetTimer, WatchCursor, 100
return
WatchCursor:
MouseGetPos, xpos, ypos, id, control
if Xpos between 600 and 800
{
if ypos between 400 and 600
ToolTip, React Here
}
else if Xpos between 200 and 400
{
if ypos between 100 and 300
ToolTip, Also React Here
}
else
ToolTip
return
Hope it helps

Autohotkey - ImageSearch

I m trying to test a GUI by comparing my current screen with an image of the required screen and seeing if they match. I m using ImageSearch of AutoHotKey for this.
CoordMode, Pixel
ImageSearch,x,y,0,0,A_ScreenWidth,A_ScreenHeight,*255 C:\Documents and Settings\XYZ\Desktop\AutoHotkey\help window.bmp
if ErrorLevel=1
Msgbox 0,Fail,Fail
if ErrorLevel=0
{
MouseClick, left, 50, 191
Sleep, 100
}
I keep getting an ErrorLevel =1. Any suggestions on how I can resolve this.
Thanks
The *255 is messing you up. This basically nullifies the image search, since everything on the screen will match the image. Try something lower. It's unlikely you should need higher than *100
Also, as a general tip, try using the smallest possible version of the image you're searching for.