How to use Window Spy with multiple monitors

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

How to use Window Spy with multiple monitors

20 Apr 2024, 17:10

I am still fairly new to AHK. I use Windows 11 and have 3 monitors. I divide each monitor in half, with a total of 6 windows. One of the main things I do is look at 6 stock charts using stockcharts.com. After I have updated a chart (with annotations) I need to save the chart. To do that I have to click on a save button near the top of the screen which brings up a dialog box in which I need to click another save button. I am trying to write a script to automate those 2 mouse clicks. I have been trying to use Window Spy to find the coordinates of each of these "save" buttons. In the "Mouse Position" pane of "Window Spy" there are many options for those coordinates. There are 4 sets within the "Mouse Position" pane, and those change depending on which of my 6 windows I am in.
Could someone tell me how to come up with one set of coordinates to use in my script so that it works whether I am in window 1, or 2, or 3, or 4, or 5, or 6?

Thank you so much.
User avatar
Seven0528
Posts: 413
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: How to use Window Spy with multiple monitors

20 Apr 2024, 17:48

 The easiest approach would be to use absolute coordinates, referring to the Screen coordinate system of Window Spy.
If the position of the monitor and the windows within it remains constant, you can simply write macros using absolute coordinates.

If that's not the case, the problem becomes a bit more complex.
You can obtain a list of HWNDs using the WinGetList function and then determine the coordinates of each window from that list using WinGetPos.
Using Window or Client coordinates requires activating the window to serve as a reference point, which adds some complexity to the problem.
In multi-monitor environments, especially when each monitor has a different scaling factor, MouseMove tends to malfunction. In such cases, consider using SetCursorPos instead.
https://www.autohotkey.com/docs/v2/lib/MouseMove.htm#Remarks

I don't believe it's necessary to determine which monitor a window belongs to in order to solve this problem.
However, if that information is truly needed...
You can use MonitorFromWindow to find out, but since it involves dealing with hMonitors, I won't elaborate further here.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

Re: How to use Window Spy with multiple monitors

20 Apr 2024, 18:19

Thank you for your quick response. When I use the absolute coordinates (with the "Screen" reading, I get a constant "Y" coordinate of 238.
However, the "X" coordinate is different for each of the 6 windows. These are the "X" coordinates for screens 1-6.
1394,-435, 526, 1487, 2446, 3406
What do I do to make a single script to use no matter which is the active window?
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

Re: How to use Window Spy with multiple monitors

20 Apr 2024, 18:31

Seven0528, I suspect you can see that there is an "X" offset from one window to the next of about 960. That seems relevant, but I am not sure what to do with it.
Also, I should note that I have the "Follow Mouse" option checked.
User avatar
Seven0528
Posts: 413
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: How to use Window Spy with multiple monitors

20 Apr 2024, 19:25

Code: Select all

dllCall("SetCursorPos", "Int",x, "Int",y) ;  mouseMove(x, y)
sleep(50)
click
 If the coordinates are predetermined, there's no need to check the active window; just use SetCursorPos directly followed by a slight delay before clicking.
For all six sets of coordinates. (SetCursorPos always uses absolute coordinates.)
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

Re: How to use Window Spy with multiple monitors

20 Apr 2024, 22:45

Either I have miscommunicated what I mean by active window or I do not understand your answer.
If the cursor is in window #1, making it the active window, the x,y are -1394,238.
If the cursor is in window #2, making it the active window, the x,y are -435,238.
If the cursor is in window #3, making it the active window, the x,y are 526,238.
If the cursor is in window #4, making it the active window, the x,y are 1487,238.
If the cursor is in window #5, making it the active window, the x,y are 2446,238.
If the cursor is in window #6, making it the active window, the x,y are 3406,238.

Does the code you gave above handle each of these cases? If so, what do I put in for the values of x and y?

Thanks
Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: How to use Window Spy with multiple monitors

20 Apr 2024, 23:08

@drbobsing
1. AHK is by default DPI system aware, meaning it thinks the screen scaling of the main monitor is the screen scaling of all monitors. If that is not the case then you will get weird coordinates or "jumps" in coordinates between monitors. To fix this, add DllCall("SetThreadDpiAwarenessContext", "ptr", -3, "ptr") to the top of your code, which makes AHK per-monitor aware.
2. Once you've done the first step, I recommend using Window coordinates from Window Spy. The default CoordMode is Window unless you've changed that. What this means is that coordinates will be relative to the top left corner of the active window and you won't need to worry about adjusting for window location, screen number etc. You can activate your target window with AHK and then perform the Click action.

These two steps won't solve the problem of windows having different scalings on monitors with different scalings. If you record your coordinates on a monitor with 100% scaling then they won't work on a monitor with 150% scaling. There are ways around that, but have you considered alternative options? Perhaps Window Spy gives you a ClassNN name for the save button and you could use ControlClick instead (omitting any need for coordinates)? UIAutomation could be another option.
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 09:11

I see "ank_class Chrome_WidgetWin_1". Is that what you are talking about? If so, what would the code look like?
Thank you so much.
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 10:09

So sorry, I did not follow your instructions.
When my mouse is on the save button I see the ClassNN to be "Chrome_RenderWidgetHostHWND".
So, would my code be:
ControlClick, Chrome_RenderWidgetHostHWND
Thanks so much.
User avatar
kunkel321
Posts: 1194
Joined: 30 Nov 2015, 21:19

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 10:33

What app has the 'save' buttons? Is it Chrome Browser, or a Windows App? AHK Has "ControlGet" which can sometimes isolate a button for pressing (no coordinates needed). It won't work in Chrome Browser web apps though. Try dragging the WinSpy crosshairs over the button to see if you can capture its information.
ste(phen|ve) kunkel
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 12:58

I think the answer is Chrome Browser. It is a StockCharts.com SharpChart which is a tab in the Chrome Browser.
The words "Chrome Legacy Window" show up under Control Under Mouse Position and next to "Text:".
They also show up under "Visible Text:" and under "All Text:"
How do I use that in the ControlClick syntax?
User avatar
kunkel321
Posts: 1194
Joined: 30 Nov 2015, 21:19

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 13:30

drbobsing wrote:
21 Apr 2024, 12:58
How do I use that in the ControlClick syntax?
The ControlClick syntax is here:
https://www.autohotkey.com/docs/v2/lib/ControlClick.htm
Unfortunately, it probably won't matter. If you were able to isolate the control, you'd know, because WinSpy would put a little highlighted rectangle around it and give you the button properties (instead of just giving you the Chrome info), when you drag the crosshairs over it.
ste(phen|ve) kunkel
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 13:44

Here are 2 attempts at the Syntax.

ControlClick, Chrome Legacy Window, Chrome_RenderWidgetHostHWND,,,, -- Chrome LEgacy Window found under Control Under Mouse Position and next to "Text:
ControlClick, SAVE, Chrome_RenderWidgetHostHWND,,,, -- SAVE found in the rectangle box when my cursor is on the Save button.

Neither seem to work. Suggestions?
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 16:33

drbobsing wrote: Here are 2 attempts at the Syntax.

Neither seem to work.
You posted in the v2 section, and all the code and links in the replies have been v2-based, including the exact page where you would find the v2 ControlClick syntax, but then you post attempts using v1 syntax. And even in that one, you are not putting parameters in the right places.

drbobsing wrote: Suggestions?
Decide which version you are using, look at the documentation page for ControlClick for that version, and follow the parameter definitions carefully.
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 18:13

Thank you boiler. I thought I was running v2 but was not.
I originally installed v1 and moved my script.ahk to a special directory which would always get backed up, so I would not lose it. I put a shortcut to this script in my startup folder.
I think that it is for that reason that even though I have installed v2, it is still running v1.
When I check properties for my script.ahk I see that it opens with autohotkey Launcher and gives me the option to change it to autohotkey Dash. If I say yes, will that switch me to v2?
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 18:18

Or should I navigate to Program Files\Autohotkey and select AutoHotkey.exe?
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 18:22

Or better yet, should I navigate to:
Program Files\AutoHotkey\v2\AutoHotkey64.exe
drbobsing
Posts: 37
Joined: 30 Apr 2022, 16:40

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 18:26

I am running an x64-based PC
Processor 12th Gen Intel(R) Core(TM) i7-12700, 2100 Mhz, 12 Core(s), 20 Logical Processor(s)
User avatar
Seven0528
Posts: 413
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 18:27

Code: Select all

#Requires AutoHotkey v1.1
#Requires AutoHotkey v2.0
 You can restrict which version of AutoHotkey to run with the #Requires directive, typically placed at the beginning of the script.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
Seven0528
Posts: 413
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: How to use Window Spy with multiple monitors

21 Apr 2024, 18:32

20240422_082946.png
(40.65 KiB) Downloaded 66 times
20240422_082958.png
(19.85 KiB) Downloaded 66 times
 You can also determine which version to launch under Launch settings.
Personally, I have both v1 and v2 installed, so I configure it like this.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: billyt, Bing [Bot] and 71 guests