How to send hotkey combinations to background Spotify using C#

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#?

Had the same problem building a media controller app. Spotify ignores keyboard messages unless it’s actually focused - most apps do this.

Here’s what worked: grab the current window with GetForegroundWindow, quickly switch focus to Spotify with SetForegroundWindow, send your Ctrl+Up/Down, then immediately restore the original window. The switch is so fast users don’t even notice.

I also tried using Windows Audio Session API to control Spotify’s volume directly. More setup work, but you get programmatic control over the volume mixer without any window tricks.

actually, just use spotify’s web api instead. they’ve got volume control endpoints that don’t need any window tricks. you’ll need to authenticate your app first, but then you can adjust volume remotely without messing with focus. way cleaner than sending keystrokes.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.