ControlSend not working

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
x-tremespeed
Posts: 29
Joined: 27 Jul 2019, 19:57

ControlSend not working

18 Oct 2019, 11:48

I'm having an issue with ControlSend being inconsistent in my code

Code: Select all

#SingleInstance, force

!^+Y::
	ControlSend, ahk_parent, !^+z, OBS
	Sleep, 2000
	MsgBox, % "Done"
	ControlSend, ahk_parent, !^+z, OBS
	MsgBox, % "Done2"
Return
This code *should* send ctrl alt shift z to OBS (my start/stop record hotkey), wait 2 seconds, then stop the recording by sending the same hotkey again. I added message boxes just to make sure the code is running through the entire thing. Both message boxes appear in the right order, but it's just as if it skips over the second ControlSend command.

Any help with this issue would be greatly appreciated!
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: ControlSend not working

18 Oct 2019, 15:26

The reason why it may not be sending it to that window could be with the title of the window. I would add a line to check to make sure the ErrorLevel is not set to 1. If it is you know the title of the window or one of the parameters to make it function are wrong. So for testing this should be what your code is.

Code: Select all

#SingleInstance, force

!^+Y::
	ControlSend, ahk_parent, !^+z, OBS
	if ErrorLevel
		MsgBox % "Error with ControlSend"
	Sleep, 2000
	MsgBox, % "Done"
	ControlSend, ahk_parent, !^+z, OBS
	MsgBox, % "Done2"
Return
x-tremespeed
Posts: 29
Joined: 27 Jul 2019, 19:57

Re: ControlSend not working

18 Oct 2019, 16:37

I ran your adjusted code and the error message doesn't pop up.

I know the ControlSend line works because the first command goes through - it's the second identical command that happens after the sleep command that doesn't go through for some reason, despite being identical to the first one.
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: ControlSend not working

18 Oct 2019, 16:39

Perhaps there is more than one window that matches the wintitle and the second key combo gets send to the wrong window - try to make the wintitle parameter more specific.
x-tremespeed
Posts: 29
Joined: 27 Jul 2019, 19:57

Re: ControlSend not working

18 Oct 2019, 16:49

I tried running this but it comes back with the error code now. I'm assuming something to do with the comma or maybe parentheses? I also tried cutting it off after the 24.0.1 bit, but that just worked the same way as with just OBS in the wintitle parameter - running the first ControlSend line fine then seemingly ignoring the second.

Code: Select all

#SingleInstance, force

!^+Y::
	ControlSend, ahk_parent, !^+z, OBS 24.0.1 (64-bit, windows) - Profile: sound record - Scenes: sound record (blank)
	if ErrorLevel
		MsgBox % "Error with ControlSend"
	Sleep, 2000
	MsgBox, % "Done"
	ControlSend, ahk_parent, !^+z, OBS 24.0.1 (64-bit, windows) - Profile: sound record - Scenes: sound record (blank)
	MsgBox, % "Done2"
Return
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: ControlSend not working

18 Oct 2019, 17:21

Like this, at least the comma needs to be escaped, I am sure - perhaps try instead:

Code: Select all

ControlSend, ahk_parent, !^+z, % "OBS 24.0.1 (64-bit, windows) - Profile: sound record - Scenes: sound record (blank)"
Ideally, I thought more of some other wintitle criteria like ahk_class (in case its unique enough) or ahk_id or a combination of different criteria:

Code: Select all

ControlSend, ahk_parent, !^+z, OBS 24 ahk_class whateverthisOBSclassiscalled
https://www.autohotkey.com/docs/misc/WinTitle.htm#multi wrote:Multiple Criteria
[...]
When using this method, the text of the title (if any is desired) should be listed first, followed by one or more additional criteria. Criteria beyond the first should be separated from the previous with exactly one space or tab (any other spaces or tabs are treated as a literal part of the previous criterion).
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: ControlSend not working

18 Oct 2019, 17:34

This should make sure that you are at least sending to the same window both times:

Code: Select all

UniqueID := WinExist("OBS 24")
ControlSend, ahk_parent, !^+z, ahk_id %UniqueID%
if ErrorLevel
	MsgBox % "Error with ControlSend"
Sleep, 2000
ControlSend, ahk_parent, !^+z, ahk_id %UniqueID%
If that's not working, then it might be a OBS-specific problem...
x-tremespeed
Posts: 29
Joined: 27 Jul 2019, 19:57

Re: ControlSend not working

18 Oct 2019, 17:43

I tried both of those options - using the ahk_class, using the class and name, using the pid, and I tried with the % and quotes. I also tried making separate functions for each part of the code like this:

Code: Select all

#SingleInstance, force

!^+Y::
	start()
	pause()
	stop()
Return

start()
{
	ControlSend, ahk_parent, !^+z, OBS
	MsgBox, % "Started"
}

pause()
{
	Sleep, 2000
	MsgBox, % "Paused"
}

stop()
{
	ControlSend, ahk_parent, !^+x, OBS
	MsgBox, % "Stopped"
}
I get the same result - the first ControlSend works, then the second one doesn't do anything.

I just ran this:

Code: Select all

!^+Y::
	start()
	if ErrorLevel
		MsgBox % "Error with ControlSend"
	start()
	if ErrorLevel
		MsgBox % "Error with ControlSend"
	start()
	if ErrorLevel
		MsgBox % "Error with ControlSend"
	start()
	if ErrorLevel
		MsgBox % "Error with ControlSend"
	start()
	if ErrorLevel
		MsgBox % "Error with ControlSend"
	stop()
	if ErrorLevel
		MsgBox % "Error with ControlSend"
Return
Even with calling the functions that many times, only the very first ControlSend command works, and no errors at all. I did, however, get a MsgBox for each function, so at least that is working!
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: ControlSend not working

18 Oct 2019, 17:46

I tried both of those options - using the ahk_class, using the class and name, using the pid, and I tried with the % and quotes.
For testing, I wouldn't use the process id pid which might have different windows, afaik, but the specific ahk_id for one specific, hopefully the right, window.

If nothing works, it might be a OBS-specific quirk.

Edit:
I just noticed that you are now using a different shortcut for stopping:

Code: Select all

ControlSend, ahk_parent, !^+x, OBS
vs. ControlSend, ahk_parent, !^+z, OBS originally .Is that intentional ?
x-tremespeed
Posts: 29
Joined: 27 Jul 2019, 19:57

Re: ControlSend not working

18 Oct 2019, 17:56

The uniqueID snippet didn't seem to work any differently either. I do have this little bit of code that does work, although it relies on having OBS as the active window.

Code: Select all

#SingleInstance, force

!^+Y::
	Send !^+Z
	Sleep, 2000
	Send !^+Z
Return
OBS is a computer streaming/recording software - most people on twitch or youtube streaming probably use this or something similar. I am trying to set it up to automatically turn off recording after a set amount of time (in the final code much longer than 2 seconds).
x-tremespeed
Posts: 29
Joined: 27 Jul 2019, 19:57

Re: ControlSend not working

18 Oct 2019, 17:59

Oh yes, the switch from z to x was intentional - I set up the x hotkey to do something other than start/stop record in the program to see if it would be more successful if it wasn't doing the same command twice, although it doesn't appear to make any difference.

Sorry for that!
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: ControlSend not working

18 Oct 2019, 18:01

Well, the AHK_id shouldn't change as long as the window exists.

Some programs don't react to ControlSend at all - as it seems, OBS changes its behaviour while it is recording and decides to ignore it then... but perhaps another forum user who has OBS already installed can take a look...
x-tremespeed
Posts: 29
Joined: 27 Jul 2019, 19:57

Re: ControlSend not working

18 Oct 2019, 18:06

Actually, if I run the hotkey when it is recording, it will work to turn it off so I don't think the state of recording/not recording is the issue.
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: ControlSend not working

18 Oct 2019, 18:11

But then you press it physically, I assume, not via ControlSend. That's not the same. Send works also differently (also depending on the send mode).
Like I said, there are programs who ignore ControlSend altogether.
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: ControlSend not working

18 Oct 2019, 18:13

Perhaps ControlClick can help... but I wouldn't bet on it.
x-tremespeed
Posts: 29
Joined: 27 Jul 2019, 19:57

Re: ControlSend not working

18 Oct 2019, 18:13

If I press my hotkey once it turns the recording on, then if I press it again it turns it off instead of each hotkey press turning it on then turning it off again - I don't click the record or stop recording button at all.
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: ControlSend not working

18 Oct 2019, 18:17

That sounds even stranger. Without actually testing it, I have no idea what is happening there 🤷‍♂️

Perhaps I'll have a go with it later...

Edit: Could it be that if the hotkey/ControlSend works, the OBS window (or one specific OBS window) is always active while it's not for the second ControlSend ?
x-tremespeed
Posts: 29
Joined: 27 Jul 2019, 19:57

Re: ControlSend not working

18 Oct 2019, 18:35

I make sure to click a different window to make obs not active every time I test the code. I'll look into the control click thing and let you know how that goes.

Thank you for all your time spent helping me so far! I appreciate it!
x-tremespeed
Posts: 29
Joined: 27 Jul 2019, 19:57

Re: ControlSend not working

24 Oct 2019, 15:39

I've been messing around with ControlSend a bit, and it seems to only want to work if it's activated directly by a hotkey, and only work once per hotkey press - multiple ControlSend commands don't work for me with a single hotkey press.

I've tried putting ControlSend in a standalone script with only that command and that doesn't work. I've tried looping it with Loop() and that doesn't work. SetTimer looping doesn't work for me either.

This doesn't work (using ControlSend, not directly activated by hotkey)

Code: Select all

#SingleInstance, force
abc:=0
!^+Y::
	MsgBox % "control recieved"
	SetTimer, startRecording, 5000
Return

startRecording:
MsgBox % "startRecording activated"
if (abc < 2){
	MsgBox % "Before controlsend"
	ControlSend, ahk_parent, !^+z, OBS 
	MsgBox % "after controlsend"
	abc++
}
else{
ExitApp
}
Return
But these two do work (using Send, not directly activated by hotkey and using ControlSend, directly activated by hotkey)

Code: Select all

#SingleInstance, force
abc:=0
!^+Y::
	MsgBox % "control recieved"
	SetTimer, startRecording, 5000
Return

startRecording:
MsgBox % "startRecording activated"
if (abc < 2){
	MsgBox % "Before controlsend"
	Send !^+Z
	MsgBox % "after controlsend"
	abc++
}
else{
ExitApp
}
Return

Code: Select all

#SingleInstance, force

!^+Y::
	ControlSend, ahk_parent, !^+z, OBS
Return
I'm still messing around with ControlClick which seems to work better! Although I'm still learning how to use it.
x-tremespeed
Posts: 29
Joined: 27 Jul 2019, 19:57

Re: ControlSend not working

24 Oct 2019, 15:51

Final Update!

I fixed my issue with ControlClick and found the final solution for my issue!

My requirements were
1. Interact with window without having focus on that window
2. Work regardless of window size etc. (ControlClick specific potential issue)

This is the code that ended up working for me:

Code: Select all

#SingleInstance, force

!^+Y::
	ControlClick, Qt5QWindowIcon1, OBS,,,, NA x66 y59
	Sleep, 2000
	ControlClick, Qt5QWindowIcon1, OBS,,,, NA x66 y59
Return
Thank you very much gregster for all your help, and for suggesting I try ControlClick - that did the trick!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], peter_ahk, Rauvagol and 339 guests