Last active
April 9, 2025 13:51
-
Star
(109)
You must be signed in to star a gist -
Fork
(1)
You must be signed in to fork a gist
-
-
Save jcsteh/7ccbc6f7b1b7eb85c1c14ac5e0d65195 to your computer and use it in GitHub Desktop.
AutoHotkey script to control Spotify with global keyboard shortcuts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; SpotifyGlobalKeys.ahk: | |
; AutoHotkey script to control Spotify with global keyboard shortcuts | |
; Author: James Teh <[email protected]> | |
; Copyright 2017-2018 James Teh | |
; License: GNU General Public License version 2.0 | |
DetectHiddenWindows, On | |
; Get the HWND of the Spotify main window. | |
getSpotifyHwnd() { | |
WinGet, spotifyHwnd, ID, ahk_exe spotify.exe | |
Return spotifyHwnd | |
} | |
; Send a key to Spotify. | |
spotifyKey(key) { | |
spotifyHwnd := getSpotifyHwnd() | |
; Chromium ignores keys when it isn't focused. | |
; Focus the document window without bringing the app to the foreground. | |
ControlFocus, Chrome_RenderWidgetHostHWND1, ahk_id %spotifyHwnd% | |
ControlSend, , %key%, ahk_id %spotifyHwnd% | |
Return | |
} | |
; Win+alt+p: Play/Pause | |
#!p:: | |
{ | |
spotifyKey("{Space}") | |
Return | |
} | |
; Win+alt+down: Next | |
#!Down:: | |
{ | |
spotifyKey("^{Right}") | |
Return | |
} | |
; Win+alt+up: Previous | |
#!Up:: | |
{ | |
spotifyKey("^{Left}") | |
Return | |
} | |
; Win+alt+right: Seek forward | |
#!Right:: | |
{ | |
spotifyKey("+{Right}") | |
Return | |
} | |
; Win+alt+left: Seek backward | |
#!Left:: | |
{ | |
spotifyKey("+{Left}") | |
Return | |
} | |
; shift+volumeUp: Volume up | |
+Volume_Up:: | |
{ | |
spotifyKey("^{Up}") | |
Return | |
} | |
; shift+volumeDown: Volume down | |
+Volume_Down:: | |
{ | |
spotifyKey("^{Down}") | |
Return | |
} | |
; Win+alt+o: Show Spotify | |
#!o:: | |
{ | |
spotifyHwnd := getSpotifyHwnd() | |
WinGet, style, Style, ahk_id %spotifyHwnd% | |
if (style & 0x10000000) { ; WS_VISIBLE | |
WinHide, ahk_id %spotifyHwnd% | |
} Else { | |
WinShow, ahk_id %spotifyHwnd% | |
WinActivate, ahk_id %spotifyHwnd% | |
} | |
Return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just figured this out after trying the stuff here and a few other scripts I found on the AHK forum.
The following will work in AutoHotkey v2 to send the shortcuts to the Spotify main window, as long as it isn't hidden/minimized, even if the miniplayer is open.
NOTE: I am using SpotX to banish the adspam. I am a premium subscriber, so I shouldn't be getting slapped in the face regularly on top of my wallet being gouged every month. Google SpotX if you're in the same boat.
Anyway, this should still work with the vanilla Spotify app.
I also have my functions in there for waiting for hotkeys to be physically released before triggering the script they should execute, and for displaying a tooltip showing the hotkey pressed along with a brief description of it.
The functions will need to be at the top of the your ahk script, in the auto-execute thread, as noted.
This should allow you to control Spotify and its volume independently of the master volume of your sytem. And it doesn't rely on the use of "media" keys, so if you have any other media players open, there won't be any mess with those being started/stopped along with or instead of Spotify.