6
votes

I have an AHK script (below) which sends various commands to the Spotify desktop app globally while in the background to perform various actions, however I play an MMO which has an always running anti-cheat which shuts the game down when it detects AHK because people can use it for macros, etc.

; "CTRL + Alt + UP"  Increase the volume.
^!Up::
DetectHiddenWindows, On
WinGet, winInfo, List, ahk_exe Spotify.exe
Loop, %winInfo%
{
    thisID := winInfo%A_Index%
    ControlFocus , , ahk_id %thisID%
    ControlSend, , ^{up}, ahk_id %thisID%
}
return

The anti-cheat does not detect/care about C# programs/applications so I'm porting over the code to C# and have found a way to send commands to the Spotify application to perform a majority of the stuff I wanted via SendMessage:

private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101; //Tried using these to no avail
private const int WM_KEYSYS = 0x0104;
private const int WM_ACTIVATEAPP = 0x001C;

var proc = Process.GetProcessesByName("Spotify").FirstOrDefault(p => !string.IsNullOrWhiteSpace(p.MainWindowTitle));
IntPtr hwnd = proc.MainWindowHandle;
SendMessage(hwnd, 0x0319, (IntPtr)0, (IntPtr)917504);

This command would play/pause the current song as that is what the final number (917504) corresponds to. Spotify also provides command codes for volume up/down, however they affect the entire volume of my PC, not just spotify which is obviously not what I want.

I've tried literally hundreds of combinations of PostMessage and SendMessage and I simply cannot figure out how to send the keys 'Ctrl' + 'Up' to make the volume increase (and vice versa for decrease). while in the background. I don't want the window to be brought to the foreground, etc.

Any help is greatly appreciated.

I also mentioned the AHK script because from my digging I think the ControlSend function is ran from this point in the source code https://github.com/Lexikos/AutoHotkey_L/blob/90b5c2ac3208c38b9488a72fc3e6e4f9cf20b276/source/keyboard_mouse.cpp#L135 , however I don't understand C/C++ enough to be able to figure out how to override the functions that require the window focus, etc.

1
Your C# code will send the same commands as AHK. So if AHK is blocked your app will be block as well. If it's not, then your game block the process name ahk_exe from running so just rename the exe fileFranck
The game doesn't seem to care what the program running the ahk script is called as it blocks it regardless of its name/what process it's running underSuperduder
So it's blocking the c++ sendkey that AHK is sending. C# sends the same exact same sendkey api. How the same behavior can yield different result ? The only way is that there is a second factor based on AHK itselfFranck
The issue isn't the anticheat preventing specific functions like SendKey being run, it's that it'll block AHK regardless of if it's renamed, moved or under a different process name. It'll also block AHK scripts if they're compiled to .exe's. I'm more focused on getting the C# code to work than worrying about the anticheat.Superduder
@Franck some games are so stupidly made that they e.g simply wont launch if you have any AHK script installed. Whoever thought of that, has some real issues. I had one game (CoD BO4) that crashed instantly upon launch with no error message if any AHK script was running. After days of trying to figure it out with Google/the game's support, I gave up. Then like a year later I saw a post about it online. Absolutely absurd the developers would do this. But anyway, more on topic, OP try if the game wont detect AHK v2, or AHK_H. I think encrypted AHK_H could have a real chance.0x464e

1 Answers

0
votes

Maybe think about compiling your AHK script to an exe: It might not be blocked this way. And you would have no effort to transfer your AHK solution.