I’m trying to control Spotify volume using C# while it runs in the background. I used to have an AutoHotkey script that worked perfectly for sending Ctrl+Up and Ctrl+Down to adjust Spotify’s internal volume, but I can’t use AHK anymore due to anti-cheat software.
I managed to get basic playback controls working with SendMessage:
const int WM_APPCOMMAND = 0x0319;
const int APPCOMMAND_MEDIA_PLAY_PAUSE = 917504;
var spotifyProcess = Process.GetProcessesByName("Spotify").FirstOrDefault(p => !string.IsNullOrEmpty(p.MainWindowTitle));
IntPtr windowHandle = spotifyProcess.MainWindowHandle;
SendMessage(windowHandle, WM_APPCOMMAND, IntPtr.Zero, (IntPtr)APPCOMMAND_MEDIA_PLAY_PAUSE);
This works great for play/pause functionality. However, I’m stuck on volume control. Spotify has built-in volume commands but they change the system volume instead of just Spotify’s internal volume slider.
What I really need is to replicate the Ctrl+Up and Ctrl+Down key combinations that Spotify recognizes for its internal volume. I’ve tried many approaches with PostMessage and SendMessage using WM_KEYDOWN, WM_KEYUP, and various virtual key codes, but nothing works when Spotify is running in the background.
The key requirement is that Spotify should NOT be brought to focus or activated. It needs to stay in the background while receiving these volume commands. Has anyone successfully sent keyboard shortcuts to background applications using C#?