Multiple ControlClicks Window PID's Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
wyw
Posts: 89
Joined: 12 Dec 2015, 19:11

Multiple ControlClicks Window PID's

15 Feb 2018, 06:32

I need my script to be working for two instances or more of the same program. I'm using Opera in this case.

I imagine it like this:
I launch Opera, I press the hotkey (F1) and the script should be doing what it's supposed to do to THAT PID, and then I open a new instance of Opera, and I press F1 once again and it does the same to that other PID. So, if I want the script to be paused, I need to be at the window itself, so that I can decide where and when to pause the script without pausing other PID's, if you know what I mean.

My script:

Code: Select all

#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.
#MaxThreadsPerHotkey 2
Settimer pressCevery7seconds, % 100 * 1000
Settimer pressReloadevery101minutes, % 500 * 1000
banana:=0
SetTitleMatchMode, 2
F1::
banana:=!banana
    while (banana=1)
	{
	    ControlClick, X121 Y321, ahk_class ahk_class Chrome_WidgetWin_1
		SetControlDelay -1
	    Controlsend,, {1}, ahk_class ahk_class Chrome_WidgetWin_1
		
    }
return
pressCevery7seconds:
	SetControlDelay 0
	ControlClick, X576 Y156, ahk_class ahk_class Chrome_WidgetWin_1
return

pressReloadevery101minutes:
	ControlClick, X148 Y1, ahk_class ahk_class Chrome_WidgetWin_1
return
f7::ExitApp 
I've tried adding something like above to my script, but without success.
Could someone help me please?
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Multiple ControlClicks Window PID's

16 Feb 2018, 17:18

Sometimes with Chrome, and I'll assume all Chromium browsers, the ahk_class can change to "Chrome_WidgetWin_1". Also, not sure that ahk_class ahk_class should be repeated, only use ahk_class.

What I would do is this for creating a list of windows to target:

Code: Select all

array:=[] ; we're going to store all our target PIDs in here
F1::
WinGet, var, PID, A
array.push(var)
Loop % array.MaxIndex()
{
WinActivate % "ahk_pid " array[A_Index]
Sleep 1000
}
return
I tested this code on four Notepad windows. After each addition, it'd loop between what I've added every second until it reached the end of the list. You can then figure out how you want to implement that amongst your code. It'll get trickier using a toggle (banana:=!banana) to start/stop the while loop but also to add more windows...
wyw
Posts: 89
Joined: 12 Dec 2015, 19:11

Re: Multiple ControlClicks Window PID's

17 Feb 2018, 07:24

Exaskryz wrote:Sometimes with Chrome, and I'll assume all Chromium browsers, the ahk_class can change to "Chrome_WidgetWin_1". Also, not sure that ahk_class ahk_class should be repeated, only use ahk_class.

What I would do is this for creating a list of windows to target:

Code: Select all

array:=[] ; we're going to store all our target PIDs in here
F1::
WinGet, var, PID, A
array.push(var)
Loop % array.MaxIndex()
{
WinActivate % "ahk_pid " array[A_Index]
Sleep 1000
}
return
I tested this code on four Notepad windows. After each addition, it'd loop between what I've added every second until it reached the end of the list. You can then figure out how you want to implement that amongst your code. It'll get trickier using a toggle (banana:=!banana) to start/stop the while loop but also to add more windows...
Thanks. That looks good. But I can't figure out how to add it to my code on my own. Could you help me here a little bit?
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Multiple ControlClicks Window PID's

17 Feb 2018, 11:19

I think it's going to be easiest to use F1 only for turning on and off the While loop.

Use a second button, like F2, to add windows. You may even add F3 to remove the currently active window if it is inside the loop. (Loop the array and compare captured variable to each value in the array, if it's a match, use array.RemoveAt(A_Index)... though if you accidentally put in multiple copies of this window, that will get screwed up. Use a Break command once you used RemoveAt and manually redo the Loop to remove the multiple instances. The other approach is to record all A_Index numbers into their own array, and then loop through this second array using the .RemoveAt() on the first.)

Then your F1 loop would look something like this:

Code: Select all

F1::
banana:=!banana
    while (banana=1)
	{
       Loop % array.MaxIndex()
        {
	    ControlClick, X121 Y321, % "ahk_pid " array[A_Index]
		SetControlDelay -1
	    Controlsend,, {1}, % "ahk_pid " array[A_Index]
        }
		
    }
return
Apply that Loop % array.MaxIndex() to the timers as well. Also, the SetControlDelay, -1 need not be repeated inside a loop, this can probably just be stuck in the auto-execute section.
wyw
Posts: 89
Joined: 12 Dec 2015, 19:11

Re: Multiple ControlClicks Window PID's

17 Feb 2018, 16:17

Exaskryz wrote:I think it's going to be easiest to use F1 only for turning on and off the While loop.

Use a second button, like F2, to add windows. You may even add F3 to remove the currently active window if it is inside the loop. (Loop the array and compare captured variable to each value in the array, if it's a match, use array.RemoveAt(A_Index)... though if you accidentally put in multiple copies of this window, that will get screwed up. Use a Break command once you used RemoveAt and manually redo the Loop to remove the multiple instances. The other approach is to record all A_Index numbers into their own array, and then loop through this second array using the .RemoveAt() on the first.)

Then your F1 loop would look something like this:

Code: Select all

F1::
banana:=!banana
    while (banana=1)
	{
       Loop % array.MaxIndex()
        {
	    ControlClick, X121 Y321, % "ahk_pid " array[A_Index]
		SetControlDelay -1
	    Controlsend,, {1}, % "ahk_pid " array[A_Index]
        }
		
    }
return
Apply that Loop % array.MaxIndex() to the timers as well. Also, the SetControlDelay, -1 need not be repeated inside a loop, this can probably just be stuck in the auto-execute section.
Damn, that seems pretty complicated to me, for real. To be honest, I have no idea regarding SetTimer.

My script looks like this now (ignore the small changes I made in the script):

Code: Select all

Settimer pressBevery5seconds, % 2 * 1000
array:=[] ; we're going to store all our target PIDs in here
F1::
WinGet, var, PID, A
array.push(var)
Loop % array.MaxIndex()
{
pressBevery5seconds:
		SetControlDelay 0
		ControlClick, X2198 Y1204, ahk_class ahk_class Chrome_WidgetWin_1
		
		pressCevery15seconds:
		SetControlDelay 0
		ControlClick, X713 Y759, ahk_class ahk_class Chrome_WidgetWin_1
        
		pressCevery13seconds:
		ControlClick, X1483 Y1682, ahk_class ahk_class Chrome_WidgetWin_1
		SetControlDelay 0
		ControlClick, X1476 Y1631, ahk_class ahk_class Chrome_WidgetWin_1
		SetControlDelay 0
		return
WinActivate % "ahk_pid " array[A_Index]
Sleep 1000
}
return
I mean, it does click, but whenever I switch windows it clicks at the windows I'm currently on (the active window).

But you are very on point! That with F2 and F3 would work and would make possible what I want although I don't know where to start (sorry for mentioning this). That would make it much easier to get it working. But to be completely honest, I can't get this to work without anyone helping me. I really want to know how to do this so I can learn more.
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Multiple ControlClicks Window PID's  Topic is solved

17 Feb 2018, 22:25

You're not utilizing the ahk_pid, so it just targets the active window when multiple windows would otherwise match "ahk_class ahk_class Chrome_WidgetWin_1".

First, ignore the timer. Get one thing working at a time.

It is not good form to include a timer label inside of a loop, so avoid it if you can. Notice that return 5 lines from the bottom, which is above WinActivate % "ahk_pid " array[A_Index]? That ends your Loop anyhow -- it fully stops it so it doesn't repeat nor execute more commands. And your timers never execute the WinActivate either.

The F1 code I gave you in the previous reply should work. If you use F2 to add windows like I did in my first reply (changing that one from F1 to F2), you can test to make sure it works on both windows.

Take it one step at a time and then build in your other features.

I don't mean to confuse you, but perhaps it would be best to just drop the While loop (banana:=!banana). Use only a timer to control those sets of Clicks. In fact, I would consider making F1 simply toggle on/off all the timers. Within the Timers you would need to Loop through the array of all windows though. What I mean is something like this, looking at pressBevery5seconds and pressCevery15seconds:

Code: Select all

SetControlDelay, 0
array:=[]
F1:: ; turn on or off timers
banana:=!banana
If banana
{
SetTimer, pressBevery5seconds, 5000 ; 2000??
SetTimer, pressCevery15seconds, 15000
}
else
{
SetTimer, pressBevery5seconds, Off
SetTimer, pressCevery15seconds, Off
}
return

F2:: ; add windows to array
WinGet, var, PID, A
array.push(var)
return

pressBevery5seconds:
Loop % array.MaxIndex()
{
ControlClick, X2198 Y1204, % "ahk_pid " array[A_Index]
}
return

pressCevery15seconds:
Loop % array.MaxIndex()
{
ControlClick, X713 Y759, % "ahk_pid " array[A_Index]
}
return
wyw
Posts: 89
Joined: 12 Dec 2015, 19:11

Re: Multiple ControlClicks Window PID's

18 Feb 2018, 06:44

My bad! It seems Opera and all other browsers I have can't have more than 1 PID. I tested your newest script using Internet Explorer, and Opera, and it worked! That with PID's sucks so bad. But your script does the job!




I've edited the script, replacing "PID" with "ahk_id ID" and it actually seems to be working (a bit). I heard all browsers can't have more than 1 PID, therefore the only way to do this is to use hwnd/ahk_id, but it's a start.

Problem is, adding "NA" does not work, the script is so fast as it is making inactive windows active windows immediately. Opera can't the causing this. I only have this problem for this specific script. I got alot of scripts for Opera, and they're not making windows active.

And I feel like I have to run two scripts at the same time due to the timer. One for one window, the other one for the other window, if you know what I mean. But this is not a must. I think that's just due to the script making unactive windows active windows, because it doesn't ControlClick. The script is literally just switching window from window.

This is how the script currently looks like:

Code: Select all

array:=[]
F1:: ; turn on or off timers
banana:=!banana
If banana
{
SetTimer, pressBevery5seconds, 1 
SetTimer, pressCevery15seconds, 1
SetTimer, pressCevery13seconds, 0
}
else
{
SetTimer, pressBevery5seconds, Off
SetTimer, pressCevery15seconds, Off
SetTimer, pressCevery13seconds, Off
}
return

F2:: ; add windows to array
WinGet, var, ID, A
array.push(var)
return

pressBevery5seconds:
Loop % array.MaxIndex()
{
SetControlDelay 0
ControlClick, X2198 Y1204, % "ahk_id " array[A_Index],,,,NA
}
return

pressCevery13seconds:
Loop % array.MaxIndex()
{
ControlClick, X431 Y1682, % "ahk_id " array[A_Index],,,,NA
SetControlDelay 0
ControlClick, X121 Y1631, % "ahk_id " array[A_Index],,,,NA
SetControlDelay 0
}
return

pressCevery15seconds:
Loop % array.MaxIndex()
{
SetControlDelay 0
ControlClick, X713 Y759, % "ahk_id " array[A_Index],,,,NA
}
return
Again, I made small changes to the script (added one timer, coordinates).







Though, I got to thank you very much for your help. I learned something off your code today. :D

Return to “Ask For Help”

Who is online

Users browsing this forum: asad41163, boiler, Google [Bot] and 203 guests