Executing ahk from the window of last mouse click

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Nishanth
Posts: 7
Joined: 04 Apr 2024, 09:11

Executing ahk from the window of last mouse click

Post by Nishanth » 04 Apr 2024, 09:17

I have a code which copies the selected text on the window that is active. I am looking for a code which will help me identify the window in which the last mouse click happened in a dual monitor setup and execute the ahk from that window.

Rohwedder
Posts: 7659
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Executing ahk from the window of last mouse click

Post by Rohwedder » 04 Apr 2024, 10:34

Hallo,
execute ahk from a window? You can only execute ahk files that are located in file folders! If there is an ahk script text in a window, it must first be saved in an ahk file before it can be executed. For this reason, ahk script text is usually not written in any window but in a window of an editor that can do the above.
You can do without an editor but not without a ahk file!
Do you want to make an ahk script from selected text in a window, if so, what role does the last mouse click play?

Nishanth
Posts: 7
Joined: 04 Apr 2024, 09:11

Re: Executing ahk from the window of last mouse click

Post by Nishanth » 15 Apr 2024, 01:39

Hello Rohwedder, Thanks for your reply. Sorry for the late response. I probably would have to give more details here. I have a dual monitor setup i.e. left monitor and right monitor. I have an icon which can be accessed on any of these monitors. The moment the user clicks on this icon an ahk script has to be executed. There could be multiple applications open on this computer, i want the script to identify the application on which the last mouse click happened and perform the next sequence of action only on that application. The next sequence of action is to copy the selected text from the application on which the last mouse click happened and save it in a temporary notepad file. I already have an ahk which copies the selected text to a temporary notepad file. The mouse click previously used to execute the ahk script directly and in that case everything was working as expected. Now the mouse click has to run a bat file which would in turn run the ahk script (this is due to other requirements in the original application) and this copies the text from the command prompt window instead of the application on which the last mouse click happened and hence it does not copy the intended text.

User avatar
xMaxrayx
Posts: 168
Joined: 06 Dec 2022, 02:56
Contact:

Re: Executing ahk from the window of last mouse click

Post by xMaxrayx » 15 Apr 2024, 04:11

just use
#HOTif windowactive ("ahk-exe xxxxx.exe")
it will executes if that window is "active"

active means when you click on it so you don't need to check where is the mouse only you need window statues.



beside im not sure if there a native official ahk method to know where your mouse at current screen, you may need DLL/work around but i'm currently doesn't use dual monitor setup but you just make it harder for you for no good reason.
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/

Nishanth
Posts: 7
Joined: 04 Apr 2024, 09:11

Re: Executing ahk from the window of last mouse click

Post by Nishanth » 15 Apr 2024, 05:08

Hi Maxray,
Thanks for your reply. The solution you suggested does not meet my requirement. I want the ahk script to execute every time the icon is clicked but ensure that it activates the window on which the last mouse click happened before it executes the script.

Rohwedder
Posts: 7659
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Executing ahk from the window of last mouse click

Post by Rohwedder » 15 Apr 2024, 06:55

Which dual monitor setup are you using?
I.e. after the key combination Win + P on active desktop, which of the modes PC screen only Duplicate Extend second screen only is selected?
What information shows Window Spy (right click AutoHotkey icon in tray) regarding this/these "icons(s) which can be accessed on any of these monitors"?

Nishanth
Posts: 7
Joined: 04 Apr 2024, 09:11

Re: Executing ahk from the window of last mouse click

Post by Nishanth » 15 Apr 2024, 07:09

I am using extend. The icon in question, is an icon available in a SCADA application. Features available within the SCADA application allows one to configure the icon to execute a windows command directly or execute a windows command by other means. Due to certain technical reasons the first option cannot be used anymore though it gives the desired result. Option 2 can be used but it does not copy the correct text because it does not consider the window on which the icon is available as the active window.

Rohwedder
Posts: 7659
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Executing ahk from the window of last mouse click

Post by Rohwedder » 16 Apr 2024, 01:07

Autohotkey can recognise that you are left-clicking.
In your case, however, it must also recognise that you are left-clicking on this SCADA icon. I don't know if the required window spy infos is enough, but without it I don't see any possibility!

Nishanth
Posts: 7
Joined: 04 Apr 2024, 09:11

Re: Executing ahk from the window of last mouse click

Post by Nishanth » 16 Apr 2024, 07:38

Do you think anything can be achieved with the help of the x y coordinates of the mouse at the time of mouse click?

Rohwedder
Posts: 7659
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Executing ahk from the window of last mouse click

Post by Rohwedder » 16 Apr 2024, 08:31

Only if this coordinate area (icon area) is the same each time. Otherwise, for example, a unique ClassNN would be better.

Nishanth
Posts: 7
Joined: 04 Apr 2024, 09:11

Re: Executing ahk from the window of last mouse click

Post by Nishanth » 16 Apr 2024, 09:33

It would remain the same all the time. What is the keyword or code which would help me achieve my requirement?

Rohwedder
Posts: 7659
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Executing ahk from the window of last mouse click

Post by Rohwedder » 16 Apr 2024, 10:57

Just a start!:

Code: Select all

#Requires AutoHotkey v2.0
~LButton:: { ; left click down
	; The moment the user clicks on this icon an ahk script has to be executed.
	Static XLeft:=275, YUp:=181, XRight:=306, YDown:=212, WinOld:=0
	CoordMode "Mouse", "Screen" ; X... Y... = Icon area
	MouseGetPos &X, &Y, &Win ; Cursor coordinates and window under cursor
	IF X > XLeft and Y > YUp and X < XRight and Y < YDown { ; if Icon is clicked
		KeyWait "LButton" ; until left click up
		A_ClipBoard := ""
		WinActivate "ahk_id" WinOld ; activates the
		;  window that was under the previous left click
		Send "^c" ; to copy the selected text
		IF ClipWait(1) ; and save it in a temporary notepad file
			FileAppend A_ClipBoard "`n" , "C:\My Documents\temporary.txt"
	} Else WinOld := Win ; remembers this window
} ;  (identify the application on which the last mouse click happened)

Nishanth
Posts: 7
Joined: 04 Apr 2024, 09:11

Re: Executing ahk from the window of last mouse click

Post by Nishanth » 16 Apr 2024, 11:14

Thank you very much!!

Post Reply

Return to “Ask for Help (v2)”