Page 1 of 1

Play/Pause in Spotify

Posted: 01 May 2017, 13:39
by RUNIE
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

Re: Play/Pause in Spotify

Posted: 01 May 2017, 13:43
by qwerty12
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

Re: Play/Pause in Spotify

Posted: 01 May 2017, 13:47
by RUNIE
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 :)

Re: Play/Pause in Spotify

Posted: 01 May 2017, 13:50
by qwerty12
Run1e wrote:I never thought of doing that, very nice.
I remember stealing the idea from some Spotify tray tip notifier. Glad it works! :)

Re: Play/Pause in Spotify

Posted: 11 Mar 2018, 07:25
by EndyBran
Thanks. This works really well. Awesome work to the guy that made this.

Re: Play/Pause in Spotify

Posted: 11 Mar 2018, 08:08
by jamesbermudean
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 :(

Re: Play/Pause in Spotify

Posted: 11 Mar 2018, 16:12
by BoBo
Downgrade?!

Re: Play/Pause in Spotify

Posted: 13 Mar 2018, 07:23
by RUNIE
Changing

Code: Select all

ahk_class SpotifyMainWindow
to

Code: Select all

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

Re: Play/Pause in Spotify

Posted: 13 Mar 2018, 10:50
by Guest
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:

Re: Play/Pause in Spotify

Posted: 14 Mar 2018, 16:07
by jamesbermudean
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:

Re: Play/Pause in Spotify

Posted: 23 Mar 2018, 17:02
by f34rtehninja
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?!?

Re: Play/Pause in Spotify

Posted: 10 Jun 2018, 06:41
by x_Wolfie
so what key am I supposed to click to pause or play a spotify song?

Re: Play/Pause in Spotify

Posted: 15 Jun 2018, 17:02
by Guest2
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!