Play/Pause in Spotify

Get help with using AutoHotkey and its commands and hotkeys
User avatar
RUNIE
Posts: 293
Joined: 03 May 2014, 14:50
GitHub: Run1e

Play/Pause in Spotify

01 May 2017, 13:39

Hey.

Currently trying to make a hotkey play/pause Spotify, and Spotify is not cooperating.

What I've tried:
The obvious approach is just to send a space to the window like:

Code: Select all

ControlSend, ahk_parent, {Space}, ahk_class SpotifyMainWindow
This does not work. The other keyboard shortcuts listed here however, does work. But not space. Nope.

The other approach, which kinda works, is using WinMenuSelectItem to select the Playback -> Play/Pause item like:

Code: Select all

WinMenuSelectItem, ahk_class SpotifyMainWindow, Chrome Legacy Window, Playback, Pause
WinMenuSelectItem, ahk_class SpotifyMainWindow, Chrome Legacy Window, Playback, Play
This does work, but not if the window is minimized (which is something the docs mention.)

Before you mention that I can just use Media_Play_Pause, no. I need the hotkey to be context specific to only Spotify. Media_Next_Pause is "grabbed" by the active window if it's listening for it.

So, have anyone got this to work? I'm using the windows Spotify client (version 1.0.53.758.gde3fc4b2)

Thanks in advance.

o/
RUNIE
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Play/Pause in Spotify

01 May 2017, 13:43

Hi,

Try the following:

Code: Select all

DetectHiddenWindows On
PostMessage, 0x319,, 0xE0000,, ahk_class SpotifyMainWindow ; msg: WM_APPCOMMAND - lParam: APPCOMMAND_MEDIA_PLAY_PAUSE
Next track: PostMessage, 0x319,, 0xB0000,, ahk_class SpotifyMainWindow

Previous track: PostMessage, 0x319,, 0xC0000,, ahk_class SpotifyMainWindow
User avatar
RUNIE
Posts: 293
Joined: 03 May 2014, 14:50
GitHub: Run1e

Re: Play/Pause in Spotify

01 May 2017, 13:47

qwerty12 wrote:Hi,

Try the following:

Code: Select all

DetectHiddenWindows On
PostMessage, 0x319,, 0xE0000,, ahk_class SpotifyMainWindow ; msg: WM_APPCOMMAND - lParam: APPCOMMAND_MEDIA_PLAY_PAUSE
Next track: PostMessage, 0x319,, 0xB0000,, ahk_class SpotifyMainWindow

Previous track: PostMessage, 0x319,, 0xC0000,, ahk_class SpotifyMainWindow
I never thought of doing that, very nice.

Thanks :)
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Play/Pause in Spotify

01 May 2017, 13:50

Run1e wrote:I never thought of doing that, very nice.
I remember stealing the idea from some Spotify tray tip notifier. Glad it works! :)
EndyBran

Re: Play/Pause in Spotify

11 Mar 2018, 07:25

Thanks. This works really well. Awesome work to the guy that made this.
User avatar
jamesbermudean
Posts: 4
Joined: 06 Oct 2017, 15:27

Re: Play/Pause in Spotify

11 Mar 2018, 08:08

qwerty12 wrote:Hi,

Try the following:

Code: Select all

DetectHiddenWindows On
PostMessage, 0x319,, 0xE0000,, ahk_class SpotifyMainWindow ; msg: WM_APPCOMMAND - lParam: APPCOMMAND_MEDIA_PLAY_PAUSE
Next track: PostMessage, 0x319,, 0xB0000,, ahk_class SpotifyMainWindow

Previous track: PostMessage, 0x319,, 0xC0000,, ahk_class SpotifyMainWindow
Thanks a lot, but this has stop working since last Spotify update :(
BoBo
Posts: 3647
Joined: 13 May 2014, 17:15

Re: Play/Pause in Spotify

11 Mar 2018, 16:12

Downgrade?!
User avatar
RUNIE
Posts: 293
Joined: 03 May 2014, 14:50
GitHub: Run1e

Re: Play/Pause in Spotify

13 Mar 2018, 07:23

Changing

Code: Select all

ahk_class SpotifyMainWindow
to

Code: Select all

ahk_exe Spotify.exe
will fix issues with the latest Spotify update.
Guest

Re: Play/Pause in Spotify

13 Mar 2018, 10:50

RUNIE wrote:Changing

Code: Select all

ahk_class SpotifyMainWindow
to

Code: Select all

ahk_exe Spotify.exe
will fix issues with the latest Spotify update.
Yeah! I also was investigating, and the reason to not work in the new update is there's no "ahk_class SpotifyMainWindow" anymore, but "ahk_class Chrome_WidgetWin_0" according to the AHK Window Spy.
I didn't know we can also use ahk_exe with PostMessage, so your solution does work perfectly (ahk_exe Spotify.exe).
Thanks a lot! :clap: :thumbup:
User avatar
jamesbermudean
Posts: 4
Joined: 06 Oct 2017, 15:27

Re: Play/Pause in Spotify

14 Mar 2018, 16:07

RUNIE wrote:Changing

Code: Select all

ahk_class SpotifyMainWindow
to

Code: Select all

ahk_exe Spotify.exe
will fix issues with the latest Spotify update.
Thanks again, the "Guest" was me, I've just noticed I didn't log in :lol:
Guest wrote:
RUNIE wrote:Changing

Code: Select all

ahk_class SpotifyMainWindow
to

Code: Select all

ahk_exe Spotify.exe
will fix issues with the latest Spotify update.
Yeah! I also was investigating, and the reason to not work in the new update is there's no "ahk_class SpotifyMainWindow" anymore, but "ahk_class Chrome_WidgetWin_0" according to the AHK Window Spy.
I didn't know we can also use ahk_exe with PostMessage, so your solution does work perfectly (ahk_exe Spotify.exe).
Thanks a lot! :clap: :thumbup:
f34rtehninja
Posts: 1
Joined: 23 Mar 2018, 16:44

Re: Play/Pause in Spotify

23 Mar 2018, 17:02

This worked great for me! Highly appreciated, the "can't skip while minimized" has really been grinding my gears.

But, rather than copy pastaing from an internet forum, I'm trying to understand what's actually going on here.

I understand the syntax of

Code: Select all

PostMessage, Msg, wParam,  lParam, Control, WinTitle
and I understand where 0x319 comes from given the documentation from the WM_APPCOMMAND docs: https://msdn.microsoft.com/en-us/librar ... s.85).aspx

But where is the 0xE0000, 0xB0000, and 0xC0000 coming from? Shouldn't it be 14, 11, 12?

According to the PostMessage docs:
The wParam and lParam parameters should be integers. If AutoHotkey or the target window is 32-bit, only the low 32 bits are used; that is, the value should be between -2147483648 and 4294967295 (0xFFFFFFFF). If AutoHotkey and the target window are both 64-bit, any integer value supported by AutoHotkey can be used. As with all integer values in AutoHotkey, a prefix of 0x indicates a hex value. For example, 0xFF is equivalent to 255.
I figured that it was a "big endian" 32-bit integer, where the number of zeroes wasn't really significant somehow. But that would mean 0xE0000000 should also work and it doesn't! Why does five significant digits work?!?
x_Wolfie
Posts: 1
Joined: 10 Jun 2018, 06:37

Re: Play/Pause in Spotify

10 Jun 2018, 06:41

so what key am I supposed to click to pause or play a spotify song?
Guest2

Re: Play/Pause in Spotify

15 Jun 2018, 17:02

x_Wolfie wrote:so what key am I supposed to click to pause or play a spotify song?
Had this question at first, but I figured it out, so thought I'd pass on the knowledge. The commands qwerty12 wrote (like PostMessage, 0x319,, 0xB0000,, ahk_class SpotifyMainWindow) are what you would assign to your hotkey. So, for example, I use F1, F2, and F3, for previous song, pause/play, and next song, respectively. My script looks like this

Code: Select all

DetectHiddenWindows, On

;play/pause
F2::PostMessage, 0x319,, 0xE0000,, ahk_exe Spotify.exe

;next song
F3::PostMessage, 0x319,, 0xB0000,, ahk_exe Spotify.exe

;previous song
F1::PostMessage, 0x319,, 0xC0000,, ahk_exe Spotify.exe
Hope this helps!
NiptheZephyr
Posts: 9
Joined: 01 Aug 2020, 21:33

Re: Play/Pause in Spotify

20 Sep 2020, 15:32

Wonderful stuff. Had been trying to figure this out with ControlSend, ahk_parent, {space}, ahk_exe Spotify.exe, but couldn't get that to work. My only problem/question is that when I do this, the Spotify window will come to front after a few hundred milliseconds. Any clue why it does this and how to get it to stop?

V/r,

NiptheZephyr

Edit: Nvm. Was going to get around it with:

Code: Select all

;WinWaitActive, ahk_exe Spotify.exe
;WinActivate, %winActWhenTriggeredVar%
but it randomly stopped doing it.

Edit2: Last update: Another way around it is noted on this site by author jcsteh: https gist.github.com /jcsteh/7ccbc6f7b1b7eb85c1c14ac5e0d65195
My simple testing of their code is below. It initially just stopped the song and then quit working, but after reloading my script and alt+tabbing a couple of times it began to work consistently. My knowledge isn't enough that I understand the wizardry of needing to call the dll twice for the third top level window. But hey, posting it here so it helps someone's knowledge at a future point!

Code: Select all

WinGet, spotifyHwnd, ID, ahk_exe spotify.exe
spotifyHwnd := DllCall("GetWindow", "uint", spotifyHwnd, "uint", 2)
spotifyHwnd := DllCall("GetWindow", "uint", spotifyHwnd, "uint", 2)
ControlFocus, Chrome_RenderWidgetHostHWND1, ahk_id %spotifyHwnd%
ControlSend, , {space}, ahk_id %spotifyHwnd%

Return to “Ask For Help”

Who is online

Users browsing this forum: famko, tatagi and 35 guests