[Help] Spotify Global Hotkeys

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
polishlessons
Posts: 2
Joined: 31 Jan 2023, 03:43

[Help] Spotify Global Hotkeys

Post by polishlessons » 01 Feb 2023, 01:09

Hi,

I found this AHK v2 script on Github so I can use global hotkeys control Spotify media functions. My use case is that the top row of my USB numpad is tied to F13-15 keys on a 2nd layer and I want to use those to control media only for Spotify. I also want to be able to use it while Spotify is minimized in the taskbar.

Code: Select all

DetectHiddenWindows True

;; Get the handle
getSpotifyHwnd() {
	spotifyHwnd := WinGetID("ahk_exe Spotify.exe")
	Return spotifyHwnd
}

; Send a key, generic
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
}

; Play/Pause
F14::{
        spotifyKey("{Space}")
        Return
}
When I press F14 on the numpad while Spotify is open it works, but when I try pressing it when its minimized in the taskbar, it spits out the following error:

Code: Select all

Error: Target control not found.

Specifically: Chrome_RenderWidgetHostHWND1

	016: {
	017: spotifyHwnd := getSpotifyHwnd()
▶	020: ControlFocus("Chrome_RenderWidgetHostHWND1", "ahk_id " . spotifyHwnd)
	021: ControlSend(key, , "ahk_id " . spotifyHwnd)
	022: Return
Any help would be appreciated, thanks.

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: [Help] Spotify Global Hotkeys

Post by mikeyww » 01 Feb 2023, 07:22

Welcome to this AutoHotkey forum!

The AHK documentation is a useful source of information for coders of all experience levels. In your case, it also provides you with the answer to your question.

ControlFocus:
To be effective, the control's window generally must not be minimized or hidden.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [Help] Spotify Global Hotkeys

Post by swagfag » 01 Feb 2023, 09:34

i wrote some time ago some UIA code for spotify viewtopic.php?p=497915
but i dont remember testing whether it worked minimized, and im not gonna retest it since ive already uninstalled that crap
u can adjust the code and see for urself, i guess

XMCQCX
Posts: 230
Joined: 14 Oct 2020, 23:44

Re: [Help] Spotify Global Hotkeys

Post by XMCQCX » 01 Feb 2023, 14:13

You can automate Spotify with the API. Didn't test it myself...
https://www.youtube.com/watch?v=FpQMv6KM48Q
https://github.com/JnLlnd/Spotify.ahk

polishlessons
Posts: 2
Joined: 31 Jan 2023, 03:43

Re: [Help] Spotify Global Hotkeys

Post by polishlessons » 02 Feb 2023, 00:49

XMCQCX wrote:
01 Feb 2023, 14:13
You can automate Spotify with the API. Didn't test it myself...
https://www.youtube.com/watch?v=FpQMv6KM48Q
https://github.com/JnLlnd/Spotify.ahk
Thanks, this solution kinda worked for me, but there was an error that occured saying something about no active player available after not playing music for a while.

For anyone coming across this thread, I found something on Github that did what i wanted for me:
https://github.com/dr-ocd/spotify_hotkeys/blob/main/spotify_hotkeys_v2-1.ahk

Post Reply

Return to “Ask for Help (v2)”