So I had this really annoying problem: whenever I hit the Pause/Play media button on my Logitech MX Keys, it would control whatever app had last played audio—sometimes a random YouTube video in a background tab—even if I had Spotify open front and centre. That really pissed me off.
All I wanted was for that key to only pause and play Spotify. Simple right?
Logitech keyboards treat those top-row keys as two things at once: a function key and a media key. You can use your keyboard to toggle between the two states. I almost always have it in the “Media key” state as I never really use function keys (f1, f2, etc). Anyway, in the Logi options app, you are able to customise those media keys and use them for whatever you like, as long as they are in the options Logitech gives you. Obviously, the option I wanted (to control a single app) wasn’t available, so I had to think of something else.

I had just started using Raycast (basically a Spotlight Search replacement) which has an awesome feature where you can run a script in the background via a hotkey. So my idea was to just set the Pause/Play button as the hotkey that runs an Apple Script. The script would tell Spotify to pause/play. Unfortunately, since the Media keys were controlled by the built in Logitech driver, I was not able to use them as hotkeys for Raycast. The alternative, was to switch the media keys into their Function Key state, so now this Pause/Play button, was F9. Guess what…Raycast also doesn’t allow Function Keys to be used as hotkeys either! What a nightmare. At this point, I knew I had to find another hacky way. Such a simple task, had turned complicated and I wasn’t going to give up.
I turned to Karabiner-Elements, which is great for low-level keyboard remapping on macOS.
Here's the full workaround:
- Via my keyboard, disable the media key functionality so F9 becomes a normal function key again.
- In Karabiner, remap F9 to a regular key combo—like ⌥⌘`—which Raycast does allow as a hotkey.
- In Raycast, bind ⌥⌘` to run an AppleScript that sends the pause/play command to Spotify.
- Set the Apple Script to run 'silently' in the background when clicked.
It’s a bit convoluted, but once it's set up, pressing F9 (which Raycast thinks is ⌥⌘`) now only controls Spotify—nothing else. No more rogue YouTube videos starting up in random tabs. I used chatGPT to write me up the Apple Script for Raycast, and I've dropped it down below.
So yeah, this ended up being one of those “why is this so complicated” problems, but if you’ve got an MX Keys and just want one damn button to only control Spotify—this works. Good luck!
