Program not receiving ControlSend, but works with other programs. Topic is solved

Ask gaming related questions (AHK v1.1 and older)
dyllan_duran
Posts: 4
Joined: 19 May 2022, 17:58

Program not receiving ControlSend, but works with other programs.

Post by dyllan_duran » 19 May 2022, 18:11

Context: Very little experience with programming, only have a few hours in python

Hey guys, so essentially due to the limitations of the game I'm making this for, all key inputs made stay within the game if that makes sense. So if I have a hotkey, Ctrl+2 for another program, when I'm in game and press Ctrl+2, the input never makes it to the program where that hotkey is bound.
I have tried:
Running both game and program as an admin,
running either program or game as an admin
AHK always ran as admin
Reinstalling AHK with UAC box checked

I've was able to make the AHK script work in a google chrome keyboard tester, but when I go to use it in the program is doesn't register the key presses.
However, when using mouse macro software, the keybind set with the software on the mouse will trigger the action (but again not in game, only when at desktop. Which is why I'm trying to use AHK to by pass this).

Here's my code:

Code: Select all

;--------------------------------------------
; Works with Chrome www.keyboardtester.com/tester.html
;--------------------------------------------

XButton1::
Sleep, 100
WinActivate, ahk_pid 8092
SetTitleMatchMode 2
SetKeyDelay, 50,50
ControlSend,ahk_parent, ^{f12}, ahk_pid 8092
Sleep, 500
WinMinimize, ahk_pid 8092
return

User avatar
mikeyww
Posts: 26612
Joined: 09 Sep 2014, 18:38

Re: Program not receiving ControlSend, but works with other programs.  Topic is solved

Post by mikeyww » 19 May 2022, 20:15

If you are activating the window, you can just use Send instead. The PID could be different in different runs, so I would avoid it and instead use a window class, window title, process name, or combination of these.

Code: Select all

winTitle = ahk_exe notepad.exe

#If WinExist(winTitle)
XButton1::
WinActivate
Send ^{F12}
WinMinimize
Return
#If

dyllan_duran
Posts: 4
Joined: 19 May 2022, 17:58

Re: Program not receiving ControlSend, but works with other programs.

Post by dyllan_duran » 19 May 2022, 21:16

I've spent 12 hours trying to get this to work, and you do it in less than one, thank you so much. If you have the time, would you be able to answer some questions I have?

Why declare wintitle a variable?
And the most baffling for me, is why it didn't need a SetKeyDelay or sleep command to work. In my testing I tried setting Xbutton1 to ^{F12} but could never get the hotkey to activate through AHK. Specifically control (^). When I used Alt I Would get a response, the first letters of the toolbar became highlighted, but that was it. I couldn't get Alt or Control to trigger with the function keys, but your edit it works flawlessly. Very confused.

Again thank you for the help much appreciated.

dyllan_duran
Posts: 4
Joined: 19 May 2022, 17:58

Re: Program not receiving ControlSend, but works with other programs.

Post by dyllan_duran » 19 May 2022, 21:38

winTitle = ahk_exe notepad.exe

#If WinExist(winTitle)
XButton1::
ControlSend,, ^{s}, %wintitle%
Return
#If

Got this to work with notepad, but then when I point it at the actual program (with ^{F12} instead of ^s), it does not work. Very strange.

dyllan_duran
Posts: 4
Joined: 19 May 2022, 17:58

Re: Program not receiving ControlSend, but works with other programs.

Post by dyllan_duran » 19 May 2022, 22:31

My brains too small to get ControlSend to work. I'm going to boil it down to the application having no controls. Doesn't work with ahk_parent as well. But making it the active window, then sending the hotkey, then minimizing does the job. Just going to be a bit frustrating with the alt-tab like hang-ups.

User avatar
mikeyww
Posts: 26612
Joined: 09 Sep 2014, 18:38

Re: Program not receiving ControlSend, but works with other programs.

Post by mikeyww » 20 May 2022, 04:50

Unfortunately, many windows simply will not respond to ControlSend. It sounds like that is the case with yours.

Why declare wintitle a variable? There is no need for it. You can use the WinTitle directly (quoted, as a function parameter). Since I was testing with a different WinTitle from yours, having a variable was convenient for my changes. Also: some readers on the forum do not understand WinTitles very well. Having a variable called winTitle might help them see its "elements" more clearly.

Why no key delay or sleep? Actually, there are such delays: they are built into AutoHotkey. The default key delay for SendEvent (the default Send mode) is 10 ms; the default windowing delay is 100 ms. I find that these work for most situations (which is probably why they are there!).

Some hotkey routines work best when triggered by a key release, instead of a key press. This would be done with a hotkey such as XButton1 Up.

Explained: SetKeyDelaySetWinDelayWinTitle

Enjoy!

Post Reply

Return to “Gaming Help (v1)”