Control YouTube Playback in Chrome with AutoHotkey

Print View Mobile View

Control YouTube Playback in Chrome with AutoHotkeyIn our last post, we saw a quick way to create a YouTube playlist by copying several video links to the clipboard with AutoHotkey. Now in this post, we have a script that would allow you to control YouTube playback in Chrome with AutoHotkey while you are working in another app. This would allow you to listen and control your favorite music playlist without having to switch to the YouTube tab each time you wish to play/pause or switch songs.

Control YouTube Playback in Chrome with AutoHotkey
Drop below script into your AHK file and reload it.

#Persistent
#NoEnv
#SingleInstance, Force
DetectHiddenWindows, On
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 2
controlID 		:= 0
return

#IfWinNotActive, ahk_exe chrome.exe

; Play/pause
+!p::
	ControlGet, controlID, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome
	
	ControlFocus,,ahk_id %controlID%

	IfWinExist, YouTube
	{
		ControlSend, Chrome_RenderWidgetHostHWND1, k , Google Chrome
		return
	}
	Loop {
		IfWinExist, YouTube
			break

		ControlSend, , ^{PgUp} , Google Chrome
		sleep 150
	}
	ControlSend, , k , Google Chrome
return

; Next video in playlist
+!n::
	ControlGet, controlID, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome
	
	ControlFocus,,ahk_id %controlID%

	IfWinExist, YouTube
	{
		ControlSend, Chrome_RenderWidgetHostHWND1, +n , Google Chrome
		return
	}
	Loop {
		IfWinExist, YouTube
			break

		ControlSend, , ^{PgUp} , Google Chrome
		sleep 150
	}
	ControlSend, , +n , Google Chrome
return

; Previous video in playlist
+!b::
	ControlGet, controlID, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome
	
	ControlFocus,,ahk_id %controlID%

	IfWinExist, YouTube
	{
		ControlSend, Chrome_RenderWidgetHostHWND1, +p , Google Chrome
		return
	}
	Loop {
		IfWinExist, YouTube
			break

		ControlSend, , ^{PgUp} , Google Chrome
		sleep 150
	}
	ControlSend, , +p , Google Chrome
return

; Seek back
+!left::
	ControlGet, controlID, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome
	
	ControlFocus,,ahk_id %controlID%

	IfWinExist, YouTube
	{
		ControlSend, Chrome_RenderWidgetHostHWND1, j , Google Chrome
		return
	}
	Loop {
		IfWinExist, YouTube
			break

		ControlSend, , ^{PgUp} , Google Chrome
		sleep 150
	}
	ControlSend, , j , Google Chrome
return

; Seek forward
+!right::
	ControlGet, controlID, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome
	
	ControlFocus,,ahk_id %controlID%

	IfWinExist, YouTube
	{
		ControlSend, Chrome_RenderWidgetHostHWND1, l , Google Chrome
		return
	}
	Loop {
		IfWinExist, YouTube
			break

		ControlSend, , ^{PgUp} , Google Chrome
		sleep 150
	}
	ControlSend, , +l , Google Chrome
return

#IfWinNotActive

Five keyboard shortcuts are available here: play/pause, next video in playlist, and previous video in playlist, seek back, and seek forward. If you are playing a single video, the next video shortcut would simply start playing the next suggested video. I’ve commented each shortcut above. You can customize it as per your liking, and also remove a particular shortcut if you don’t plan on using it. To add a new shortcut, you can refer this page for the list of shortcuts supported by YouTube.

There are a few points to make note of while using this script due to restrictions implemented in Chrome:
1. If possible, have YouTube as the first tab. Not necessary, but I have seen this gives the fastest response.
2. Have all the tabs in a single Chrome window.

One thought on “Control YouTube Playback in Chrome with AutoHotkey”

Comments are closed.