I'm not able to get ControlClick to work on second monitor Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CrobblerCat
Posts: 8
Joined: 06 Jun 2021, 10:24

I'm not able to get ControlClick to work on second monitor

Post by CrobblerCat » 26 May 2022, 09:49

I've been trying everything. I've searched Google dozens of times and have tried entire scripts that I've found and also cobbled some together from bits and pieces that I've found, but nothing has worked.

All I want to do is left click coordinates (or an image match if that is easier) on my second monitor without losing focus on my current window. The coordinates never change, and the window (Chrome) is always maximized on the second screen, so the place I want clicked is always visible and unobstructed, if that matters.

I'd normally show the code I'm trying to work with here, but as mentioned, I've tried everything and none of it has worked, so I don't actually have anything.

I do have a screenshot of window spy that I took when my mouse was over the element I want to click with ControlSend (It's a play/pause button - I can't use media keys for this element which is why I require this script). I also had the window (Chrome) highlighted:

AutoHotkey_FFSuHYJb3O.png
AutoHotkey_FFSuHYJb3O.png (49.77 KiB) Viewed 920 times

If anyone could help with this, I'd be eternally grateful. I'm happy to answer any questions or provide any additional info if required.

Edit: I suppose I can share a script I'm currently using to send keystrokes to the same window. It works perfectly for keystrokes, but trying to repurpose it for ControlClicks instead of ControlSend has not been successful. Maybe this can help with getting something working:

Code: Select all

#IfWinNotActive, ahk_exe chrome.exe

!+NumpadRight::
	; Gets the control ID of google chrome
	ControlGet, controlID, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome

	; Focuses on chrome without breaking focus on what you're doing
	ControlFocus,,ahk_id %controlID%
	
	ControlSend, Chrome_RenderWidgetHostHWND1, ^+{f}, Google Chrome
return

#IfWinNotActive

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: I'm not able to get ControlClick to work on second monitor

Post by boiler » 26 May 2022, 11:10

It doesn't make sense that your coordinates are negative for the window. Negative coordinates would never be correct for use with ControlClick. The window for whose coordinates you are trying to get need to be from the active window, so they would be positive because they would be relative to that window's upper-left corner.

CrobblerCat
Posts: 8
Joined: 06 Jun 2021, 10:24

Re: I'm not able to get ControlClick to work on second monitor

Post by CrobblerCat » 26 May 2022, 11:25

boiler wrote:
26 May 2022, 11:10
It doesn't make sense that your coordinates are negative for the window. Negative coordinates would never be correct for use with ControlClick. The window for whose coordinates you are trying to get need to be from the active window, so they would be positive because they would be relative to that window's upper-left corner.
That seems to have had something to do with the fact that I had "Follow Mouse" checked. I figured the negatives were because my second monitor is to the left of my main monitor and it considered 0 as the left most part of my primary screen, but I suppose that wouldn't make sense with the "Window" values.

Anyway, I have an updated screenshot without "Follow Mouse" checked below. I can confirm that I also tried these coordinates previously with no success.
AutoHotkey_DfAdAR3m2Q.png
AutoHotkey_DfAdAR3m2Q.png (42.89 KiB) Viewed 899 times

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: I'm not able to get ControlClick to work on second monitor

Post by RussF » 26 May 2022, 11:27

It would make sense if the window in focus was not the window trying to be controlled. I tested with Window Spy in focus on my right-hand monitor and placed the cursor on a window on the left-hand monitor. Unless you focus the window you want on the left monitor, you will get negative window coordinates. I suggest trying Window Spy again, click follow mouse, and then put the window you want into focus. That should give you positive coordinates.

Russ
image.png
image.png (15.06 KiB) Viewed 902 times

CrobblerCat
Posts: 8
Joined: 06 Jun 2021, 10:24

Re: I'm not able to get ControlClick to work on second monitor

Post by CrobblerCat » 26 May 2022, 11:43

RussF wrote:
26 May 2022, 11:27
It would make sense if the window in focus was not the window trying to be controlled. I tested with Window Spy in focus on my right-hand monitor and placed the cursor on a window on the left-hand monitor. Unless you focus the window you want on the left monitor, you will get negative window coordinates. I suggest trying Window Spy again, click follow mouse, and then put the window you want into focus. That should give you positive coordinates.

Russ
image.png
Thanks, I posted an updated screenshot above your post, but I think my posts have to be validated by a moderator so it wasn't visible when you posted. I can confirm that those coordinates are accurate for the window and area I need to ControlClick.

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: I'm not able to get ControlClick to work on second monitor

Post by RussF » 26 May 2022, 12:31

Do you have

Code: Select all

CoordMode, Mouse, Window
set in your script?

Russ

CrobblerCat
Posts: 8
Joined: 06 Jun 2021, 10:24

Re: I'm not able to get ControlClick to work on second monitor

Post by CrobblerCat » 26 May 2022, 12:33

RussF wrote:
26 May 2022, 12:31
Do you have

Code: Select all

CoordMode, Mouse, Window
set in your script?

Russ
Yes, I tried that several times.

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: I'm not able to get ControlClick to work on second monitor  Topic is solved

Post by boiler » 26 May 2022, 13:02

CoordMode doesn't do anything for ControlClick. ControlClick is already based on the window coordinates.

What you need to make sure is that the window that you will be trying to control is the active window when you're taking the coordinates from the Window Spy tool. And the coordinates you get need to be the Window coordinates, which will not be negative if you have the window of interest active. If you move the mouse to the upper-left corner of the active window, you should see the Window coordinates converge on 0,0.

I haven't seen you post a ControlClick statement from your script yet. Can you do that?

What you're showing for the WinTitle in your ControlSend statements wouldn't find the Chrome window unless you have set SetTitleMatchMode, 2 because the window title does not begin with Google Chrome so you need the option where it will match anywhere in the title.

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: I'm not able to get ControlClick to work on second monitor

Post by RussF » 26 May 2022, 13:26

With your post visible in my browser, I obtained the coordinates of the forum's search window. I focused on another window and pressed ^f10 to execute the following code:

Code: Select all

^f10::
   ControlClick, x257 y404, ahk_exe chrome.exe
   Return
The cursor was placed into the field and focus remained on the other window. That's the best I can do for now.

Russ
image.png
image.png (39.7 KiB) Viewed 853 times

CrobblerCat
Posts: 8
Joined: 06 Jun 2021, 10:24

Re: I'm not able to get ControlClick to work on second monitor

Post by CrobblerCat » 26 May 2022, 13:31

boiler wrote:
26 May 2022, 13:02
CoordMode doesn't do anything for ControlClick. ControlClick is already based on the window coordinates.

What you need to make sure is that the window that you will be trying to control is the active window when you're taking the coordinates from the Window Spy tool. And the coordinates you get need to be the Window coordinates, which will not be negative if you have the window of interest active. If you move the mouse to the upper-left corner of the active window, you should see the Window coordinates converge on 0,0.

I haven't seen you post a ControlClick statement from your script yet. Can you do that?

What you're showing for the WinTitle in your ControlSend statements wouldn't find the Chrome window unless you have set SetTitleMatchMode, 2 because the window title does not begin with Google Chrome so you need the option where it will match anywhere in the title.
That was it! A dumb mistake on my part, which I figured would be the case. I was testing it out as its own script before I integrated it into my main AHK script and I'd stupidly forgotten to copy across all of that info from the start of my main file. The "SetTitleMatchMode, 2" was the magic bullet. For people from the future who come here via Google, here is the working script:

Code: Select all

; Keeps script permanently running
#Persistent

; Avoids checking empty variables to see if they are environment variables
; Recommended for performance and compatibility with future AutoHotkey releases
#NoEnv

; Ensures that there is only a single instance of this script running
#SingleInstance, Force

;Determines whether invisible windows are "seen" by the script
DetectHiddenWindows, On

; Makes a script unconditionally use its own folder as its working directory
; Ensures a consistent starting directory
SetWorkingDir %A_ScriptDir%

; sets title matching to search for "containing" isntead of "exact"
SetTitleMatchMode, 2

;sets controlID to 0 every time the script is reloaded
controlID	   := 0

return

;============================== CLICK BUTTON ON OTHER MONITOR ==============================
#IfWinNotActive, ahk_exe chrome.exe

F1::
; Gets the control ID of google chrome
	ControlGet, controlID, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome

	; Focuses on chrome without breaking focus on what you're doing
	ControlFocus,,ahk_id %controlID%
	
	ControlClick, x1330 y1565 Chrome_RenderWidgetHostHWND1, Google Chrome
return

#IfWinNotActive
Thank you again, I really appreciate your help!


RussF wrote:
26 May 2022, 13:26
With your post visible in my browser, I obtained the coordinates of the forum's search window. I focused on another window and pressed ^f10 to execute the following code:

Code: Select all

^f10::
   ControlClick, x257 y404, ahk_exe chrome.exe
   Return
The cursor was placed into the field and focus remained on the other window. That's the best I can do for now.

Russ

image.png
All resolved now, the cause was silliness and lack of attention to detail on my part. Thanks for sticking with my issue, I appreciate your time.

Post Reply

Return to “Ask for Help (v1)”