Last active
June 21, 2024 14:44
-
-
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 <jamie@jantrid.net> | |
; 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
This code works fine for me with Spotify minimized in tray (!), much to my delight (finally! After months of agony!)
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
}
; Ctrl+Shift+q: Play/Pause
^+q::
{
spotifyKey("{Space}")
Return
}
; alt+v: Next
!v::
{
spotifyKey("^{Right}")
Return
}
; alt+z: Previous
!z::
{
spotifyKey("^{Left}")
Return
}
; Ctrl+alt+Up: Volume up
^!Up::
{
spotifyKey("^{Up}")
Return
}
; Ctrl+alt+Down: Volume Down
^!Down::
{
spotifyKey("^{Down}")
Return
}
; Ctrl+
: Show Spotify ^
::{
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
}
Sometimes it doesn't catch the focus or on the contrary, holds it, but it already trifles for a couple clicks :)