Replicating EarPods functionality onto PC Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Darkeh
Posts: 2
Joined: 25 Dec 2021, 21:21

Replicating EarPods functionality onto PC

25 Dec 2021, 21:33

Hey, I've recently downloaded AHK and am very amateur in script-making, However, I was able to successfully associate double tap and triple tap as next and previous track for Spotify. Now I need a bit of assistance with my code to incorporate 2 features to make this script really shine. I want to bind [double tap and hold] to fast forward 10 seconds and [triple tap and hold] to rewind 10 seconds. In Spotify, the keybind combos are Ctrl + Right arrow and Ctrl + Left arrow, respectively, to achieve fast forward and rewind of 10 seconds. So I am thinking by doing the binds I can accomplish a key combo without an issue. I would also want to make holding the EarPods button an instant launch of Spotify from wherever I'm doing. Any pointers would be greatly appreciated!

Code: Select all

$vkB3::
If !TvkB3
	SetTimer, TvkB3, -400
TvkB3++
Return
TvkB3:
If TvkB3 = 3
	Send, {Media_Prev}
Else If TvkB3 = 2
	Send, {Media_Next}
Else If TvkB3 = 1
	Send, {Media_Play_Pause}
TvkB3 = 
Return
This code is not mine, it was made by another forum user, all I did was modify the attributes to follow my needs.
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Replicating EarPods functionality onto PC

26 Dec 2021, 00:05

Code: Select all

$Media_Play_Pause::
Hotkey, IfWinActive
Hotkey, Media_Play_Pause Up, On
HoldEnough := false
if !ClicksCount
	SetTimer, NoHold, -450
TimeStamp := A_TickCount
SetTimer, Hold, -150  ; Hold at minimum 150 ms
KeyWait, Media_Play_Pause ; It is how we prevent re-launching of this subroutine while the button isn't released.
return 

Media_Play_Pause Up::   ; Released before timeout.
SetTimer, Hold, Off
ClicksCount++
return

Hold:  ; Was hold for at least 150 ms
Hotkey, IfWinActive
Hotkey, Media_Play_Pause Up, Off
SetTimer, NoHold, Off
HoldEnough := true
Switch ++ClicksCount 
{
Case 2: Send, ^{Right}	; fast forward 10 seconds in Spotify
Case 3:	Send, ^{Left}	; rewind 10 seconds in Spotify
Case "", 1: MsgBox, You've pressed once and hold Media_Play_Pause button.`nYou can bind some function for this free case `;-).
}
ClicksCount := 0
return

NoHold:    ; Timed out 450 ms before release.
if GetKeyState("Media_Play_Pause", "P")  ; If the button isn't released yet.
{	Sleep, % (171 + TimeStamp - A_TickCount)  ; It is always positive, otherwise we won't be here.
	if HoldEnough
		return
}
Switch ClicksCount 
{
Case 3:	Send, {Media_Prev}
Case 2:	Send, {Media_Next}
Case 1:	Send, {Media_Play_Pause}
}
ClicksCount := 0
return
EDIT reason:
Deleted useless piece of code:
else {
SetTimer, Hold, Off
Hotkey, IfWinActive
Hotkey, Media_Play_Pause Up, Off
}
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Replicating EarPods functionality onto PC  Topic is solved

26 Dec 2021, 00:51

About sending Ctrl+Right and Ctrl+Left to Spotify. I need to know WinTitle of Spotify window. Can you tell me it? What is the name of an exe-file? What is the caption of its window?
Otherwise my code will fast forward and rewind only if Spotify window is active (has focus).
If the process name is Spotify.exe, you can try this:
Spoiler
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
Darkeh
Posts: 2
Joined: 25 Dec 2021, 21:21

Re: Replicating EarPods functionality onto PC

26 Dec 2021, 20:29

amateur+ wrote:
26 Dec 2021, 00:51
About sending Ctrl+Right and Ctrl+Left to Spotify. I need to know WinTitle of Spotify window. Can you tell me it? What is the name of an exe-file? What is the caption of its window?
Otherwise my code will fast forward and rewind only if Spotify window is active (has focus).
If the process name is Spotify.exe, you can try this:
Spoiler
This code works amazingly! I was thrown off as to why the fast forward and rewind of 10 seconds didn't work, but I realized that Spotify doesn't even have that feature, it's simple skip or previous :facepalm: If the keybinds (double tap hold/triple tap hold) do exist though, I am sure I can use them for other features! Thank you so much for the script!
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Replicating EarPods functionality onto PC

28 Dec 2021, 15:50

Darkeh wrote:
26 Dec 2021, 20:29
This code works amazingly! I was thrown off as to why the fast forward and rewind of 10 seconds didn't work, but I realized that Spotify doesn't even have that feature, it's simple skip or previous :facepalm: If the keybinds (double tap hold/triple tap hold) do exist though, I am sure I can use them for other features! Thank you so much for the script!
Yesterday I read about Spotify on Wiki and decided to install it. Nice service, I'll use it, thanks! :)
What I can say, ControlSend, , ^{Right}, ahk_exe Spotify.exe command wouldn't work with Spotify anyway because its app for Windows is based on chromium (although I tested the code successfully on AIMP.exe that got ff and rewind for Ctrl+Right and Ctrl+Left). Chromiums require its control to have focus to be able to recieve a key have been sent to it. To send keys to it we could use this code (lets take Ctrl+S for Shuffle and Ctrl+R for Repeat toggles):

Code: Select all

...
Switch ClicksCount 
{
Case 3: ChromiumSendKey("^s", "ahk_exe Spotify.exe")	; toggle Shuffle in Spotify
Case 2: ChromiumSendKey("^r", "ahk_exe Spotify.exe")	; toggle Repeat in Spotify.
...
}
ClicksCount := 0
return

ChromiumSendKey(pKey, pTitle) {
   SetTitleMatchMode, 2
   if (Hwnd := WinExist(pTitle))
   {	if WinActive("ahk_id " . Hwnd)
			Send, % pKey
		else {
			; Chromium ignores keys when it isn't focused.
			; Focus the document window without bringing the app to the foreground.
			ControlFocus, Chrome_RenderWidgetHostHWND1, % "ahk_id " . Hwnd
			Sleep 20
			ControlSend, , % pKey,  % "ahk_id " . Hwnd
		}
   }
   return Hwnd
}
As for your intention to use this multipress/hold construction for some other buttons, I think, you should come back to this forum topic a little bit later, I'll write and post here code with universal blocks to add such hotkeys easily, very quickly and with not that many lines of code if you use different keys in this manner (Media_Play_Pause, PrintScreen, Pause and maybe others).
For example it could be possible to combine blocks of several such hotkeys in one universal block of code of the same length
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: balawi28, Bing [Bot], Chunjee, moltenchees and 271 guests