Control media without focusing on the window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
akirofe
Posts: 149
Joined: 05 Apr 2021, 21:54

Control media without focusing on the window

Post by akirofe » 18 Jan 2023, 01:32

Hi guys,

[ PS. If this doesn't sound offensive, I am more than happy to pay for your time to help me, as I understand this is a lot of works. Please pm me please. Thank you very much!! ]

Someone helped me with the code below (I learned later that I should record names of those good Samaritans) to stop and play a YouTube video running on a Chrome tab while using a different program and without having to focus on that Chrome window/tab.

But I may have more than one YouTube tabs opening...

Could you please help me with changing it so it'll work with Firefox instead, or only the Chrome YouTube tab that has a certain keyword(s), or VLC,..?

It also doesn't work sometimes... I don't know why... :(

Also, when I tried changing to have ^w to send "Left" (to rewind 10 seconds) it didn't work :(

Thank you very much!!


Code: Select all


;============================== Start Auto-Execution Section ==============================

; Keeps script permanently running
#Persistent

; Avoids checking empty variables to see if they are environment variables
; Recommended for performance and compatibility with future AutoHotkey releases
#NoEnv

; Ensures that there is only a single instance of this script running
#SingleInstance, Force

;Determines whether invisible windows are "seen" by the script
DetectHiddenWindows, On

; Makes a script unconditionally use its own folder as its working directory
; Ensures a consistent starting directory
SetWorkingDir %A_ScriptDir%

; sets title matching to search for "containing" isntead of "exact"
SetTitleMatchMode, 2

;sets controlID to 0 every time the script is reloaded
controlID       := 0

return

;============================== Main Script ==============================
#IfWinNotActive, ahk_exe chrome.exe

^q::
    ; Gets the control ID of google chrome
    ControlGet, controlID, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome

    ; Focuses on chrome without breaking focus on what you're doing
    ControlFocus,,ahk_id %controlID%

    ; Checks to make sure YouTube isn't the first tab before starting the loop
    ; Saves time when youtube is the tab it's on
    IfWinExist, YouTube
    {
        ControlSend, Chrome_RenderWidgetHostHWND1, k , Google Chrome
        return
    }

    ; Sends ctrl+1 to your browser to set it at tab 1
    ControlSend, , ^1, Google Chrome

    ; Starts loop to find youtube tab
    Loop
    {
        IfWinExist, YouTube
        {
            break
        }

        ;Scrolls through the tabs.
        ControlSend, ,{Control Down}{Tab}{Control Up}, Google Chrome

        ; if the script acts weird and is getting confused, raise this number
        ; Sleep, is measures in milliseconds. 1000 ms = 1 sec
        Sleep, 150
    }

    Sleep, 50

    ; Sends the K button to chrome
    ; K is the default pause/unpause of YouTube (People think space is. Don't use space!
    ; It'll scroll you down the youtube page when the player doesn't have focus.
    ControlSend, Chrome_RenderWidgetHostHWND1, k , Google Chrome
return

#IfWinNotActive

;============================== End Script ==============================
*** Thank you for reading. I am not in coding and know almost nothing about professional coding, hope for your patience and deeply appreciate any of your kind helps. My current interest in this awesome AHK is due to that my work is graphical ((architect/CAD) and, to reduce strains, my right hand is better off not leaving the mouse (an MMO mouse that has 12 side keys which I maps a lot of F keys and other keys in) as much as possible. All the best you lovely coders! ***

ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: Control media without focusing on the window

Post by ananthuthilakan » 30 Jan 2023, 14:45

the below code will work both for firefox and VLC ,
i made this script long time ago.
Hope this helps

Code: Select all

#SingleInstance,Force

;############################################
; author : discretecourage#0179
;############################################
; How to use (works both with vlc and firefox)
;############################################
; f6 ==> change vlc into borderless
; F7 ==> rewind
; (Alt + Space) or F8 ==> pause 
; F9 ==> forward
; F10 ==> change the position of vlc if you saved the from tray icon
;############################################


SetWorkingDir %A_ScriptDir%
;~ Menu, Tray, Icon, play.ico
Menu,Tray, NoStandard


Menu, SaveCurrentPosID, add, Profile_1, Profile_1
Menu, SaveCurrentPosID, add, Profile_2, Profile_2
Menu, SaveCurrentPosID, add, Profile_3, Profile_3
Menu, SaveCurrentPosID, add, Profile_4, Profile_4
Menu, SaveCurrentPosID, add, Profile_5, Profile_5
Menu, Tray, add, Save Current Position, :SaveCurrentPosID
Menu, SetDefaultPosID, add, Set_1, Set_1
Menu, SetDefaultPosID, add, Set_2, Set_2
Menu, SetDefaultPosID, add, Set_3, Set_3
Menu, SetDefaultPosID, add, Set_4, Set_4
Menu, SetDefaultPosID, add, Set_5, Set_5
Menu, Tray, add, Set Default, :SetDefaultPosID
Menu, Tray, Add,  Restart, Reloadlbl
Menu, Tray, Add,  close, Terminate



IniRead,DefaultPos,VLCPowerTools.ini,Default,DefaultPos,1
Loop,5
IniRead,Pos_%A_Index%,VLCPowerTools.ini,Positons,Pos_%A_Index%,0|0|550|350
Menu, SetDefaultPosID, Check, Set_%DefaultPos%
return

Set_1:
Set_2:
Set_3:
Set_4:
Set_5:
loop,5
Menu, SetDefaultPosID, UnCheck, Set_%A_Index%
Menu, SetDefaultPosID, Check, %A_ThisLabel%
t:=SubStr(A_ThisLabel, 5)
DefaultPos:=t
IniWrite,%t%,VLCPowerTools.ini,Default,DefaultPos
t:=""
return


Profile_1:
Profile_2:
Profile_3:
Profile_4:
Profile_5:
t:=SubStr(A_ThisLabel, 9)
WinGetPos, tX, tY, tW, tH, ahk_exe vlc.exe
Temp:= tX "|" tY "|" tW "|" tH
if (tX!="") {
Pos_%t%:= tX "|" tY "|" tW "|" tH
IniWrite,%Temp%,VLCPowerTools.ini,Positons,Pos_%t%
}
Else
{
MsgBox, 16, VLC Not Found?, Cant get VLC Position from screen
}
return




Reloadlbl:
Reload
return

SaveCurrentPos:
WinGetPos, X, Y, W, H, ahk_exe vlc.exe
Temp:= X "|" Y "|" W "|" H
if (X!="") {

}

Return

Terminate:
ExitApp
return

F10::
DefaultPos:= (DefaultPos=5) ? 1 : DefaultPos+1
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1],strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3],strsplit(Pos_%DefaultPos%,"|")[4]
return

#If !WinActive("ahk_exe vlc.exe")
f6::

ControlSend,Qt5QWindowIcon7,^{h},ahk_exe vlc.exe
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]-1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]-1,strsplit(Pos_%DefaultPos%,"|")[4]
WinSet, Style,  ^0xC40000, ahk_exe vlc.exe
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]+1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]+1,strsplit(Pos_%DefaultPos%,"|")[4]

return
#if

#If WinActive("ahk_exe vlc.exe")
f6::

SendInput, ^h 
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]-1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]-1,strsplit(Pos_%DefaultPos%,"|")[4]
WinSet, Style,  ^0xC40000, ahk_exe vlc.exe
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]+1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]+1,strsplit(Pos_%DefaultPos%,"|")[4]
return
#if

#If (!WinActive("ahk_exe vlc.exe")) 
!Space::
F8:: ;Set your hotkey to play/pause here
ControlSend, ahk_parent, {Space}, ahk_exe firefox.exe
ControlSend,Qt5QWindowIcon7,{space},ahk_exe vlc.exe ;Send space to VLC player control
return

F9::
ControlSend,Qt5QWindowIcon7,+{Right},ahk_exe vlc.exe ;forward
ControlSend, ahk_parent, {Right}, ahk_exe firefox.exe
return

F7::
ControlSend, ahk_parent, {Left}, ahk_exe firefox.exe
ControlSend,Qt5QWindowIcon7,+{Left},ahk_exe vlc.exe ;backward
return
#if

#If WinActive("ahk_exe vlc.exe")
!Space::
F8::
SendInput, {Space}
return

F9::
SendInput, +{Right}
return

F7::
SendInput, +{Left}
return
#if


akirofe
Posts: 149
Joined: 05 Apr 2021, 21:54

Re: Control media without focusing on the window

Post by akirofe » 04 Feb 2023, 20:19

Hi ananthuthilakan,

Thank you very much!!!!!! I really appreciate it!!! I will try soon!!!!

Love you!!!
ananthuthilakan wrote:
30 Jan 2023, 14:45
the below code will work both for firefox and VLC ,
i made this script long time ago.
Hope this helps

Code: Select all

#SingleInstance,Force

;############################################
; author : discretecourage#0179
;############################################
; How to use (works both with vlc and firefox)
;############################################
; f6 ==> change vlc into borderless
; F7 ==> rewind
; (Alt + Space) or F8 ==> pause 
; F9 ==> forward
; F10 ==> change the position of vlc if you saved the from tray icon
;############################################


SetWorkingDir %A_ScriptDir%
;~ Menu, Tray, Icon, play.ico
Menu,Tray, NoStandard


Menu, SaveCurrentPosID, add, Profile_1, Profile_1
Menu, SaveCurrentPosID, add, Profile_2, Profile_2
Menu, SaveCurrentPosID, add, Profile_3, Profile_3
Menu, SaveCurrentPosID, add, Profile_4, Profile_4
Menu, SaveCurrentPosID, add, Profile_5, Profile_5
Menu, Tray, add, Save Current Position, :SaveCurrentPosID
Menu, SetDefaultPosID, add, Set_1, Set_1
Menu, SetDefaultPosID, add, Set_2, Set_2
Menu, SetDefaultPosID, add, Set_3, Set_3
Menu, SetDefaultPosID, add, Set_4, Set_4
Menu, SetDefaultPosID, add, Set_5, Set_5
Menu, Tray, add, Set Default, :SetDefaultPosID
Menu, Tray, Add,  Restart, Reloadlbl
Menu, Tray, Add,  close, Terminate



IniRead,DefaultPos,VLCPowerTools.ini,Default,DefaultPos,1
Loop,5
IniRead,Pos_%A_Index%,VLCPowerTools.ini,Positons,Pos_%A_Index%,0|0|550|350
Menu, SetDefaultPosID, Check, Set_%DefaultPos%
return

Set_1:
Set_2:
Set_3:
Set_4:
Set_5:
loop,5
Menu, SetDefaultPosID, UnCheck, Set_%A_Index%
Menu, SetDefaultPosID, Check, %A_ThisLabel%
t:=SubStr(A_ThisLabel, 5)
DefaultPos:=t
IniWrite,%t%,VLCPowerTools.ini,Default,DefaultPos
t:=""
return


Profile_1:
Profile_2:
Profile_3:
Profile_4:
Profile_5:
t:=SubStr(A_ThisLabel, 9)
WinGetPos, tX, tY, tW, tH, ahk_exe vlc.exe
Temp:= tX "|" tY "|" tW "|" tH
if (tX!="") {
Pos_%t%:= tX "|" tY "|" tW "|" tH
IniWrite,%Temp%,VLCPowerTools.ini,Positons,Pos_%t%
}
Else
{
MsgBox, 16, VLC Not Found?, Cant get VLC Position from screen
}
return




Reloadlbl:
Reload
return

SaveCurrentPos:
WinGetPos, X, Y, W, H, ahk_exe vlc.exe
Temp:= X "|" Y "|" W "|" H
if (X!="") {

}

Return

Terminate:
ExitApp
return

F10::
DefaultPos:= (DefaultPos=5) ? 1 : DefaultPos+1
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1],strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3],strsplit(Pos_%DefaultPos%,"|")[4]
return

#If !WinActive("ahk_exe vlc.exe")
f6::

ControlSend,Qt5QWindowIcon7,^{h},ahk_exe vlc.exe
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]-1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]-1,strsplit(Pos_%DefaultPos%,"|")[4]
WinSet, Style,  ^0xC40000, ahk_exe vlc.exe
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]+1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]+1,strsplit(Pos_%DefaultPos%,"|")[4]

return
#if

#If WinActive("ahk_exe vlc.exe")
f6::

SendInput, ^h 
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]-1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]-1,strsplit(Pos_%DefaultPos%,"|")[4]
WinSet, Style,  ^0xC40000, ahk_exe vlc.exe
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]+1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]+1,strsplit(Pos_%DefaultPos%,"|")[4]
return
#if

#If (!WinActive("ahk_exe vlc.exe")) 
!Space::
F8:: ;Set your hotkey to play/pause here
ControlSend, ahk_parent, {Space}, ahk_exe firefox.exe
ControlSend,Qt5QWindowIcon7,{space},ahk_exe vlc.exe ;Send space to VLC player control
return

F9::
ControlSend,Qt5QWindowIcon7,+{Right},ahk_exe vlc.exe ;forward
ControlSend, ahk_parent, {Right}, ahk_exe firefox.exe
return

F7::
ControlSend, ahk_parent, {Left}, ahk_exe firefox.exe
ControlSend,Qt5QWindowIcon7,+{Left},ahk_exe vlc.exe ;backward
return
#if

#If WinActive("ahk_exe vlc.exe")
!Space::
F8::
SendInput, {Space}
return

F9::
SendInput, +{Right}
return

F7::
SendInput, +{Left}
return
#if

*** Thank you for reading. I am not in coding and know almost nothing about professional coding, hope for your patience and deeply appreciate any of your kind helps. My current interest in this awesome AHK is due to that my work is graphical ((architect/CAD) and, to reduce strains, my right hand is better off not leaving the mouse (an MMO mouse that has 12 side keys which I maps a lot of F keys and other keys in) as much as possible. All the best you lovely coders! ***

akirofe
Posts: 149
Joined: 05 Apr 2021, 21:54

Re: Control media without focusing on the window

Post by akirofe » 06 Feb 2023, 03:24

Hi ananthuthilakan,

I will PM you for payment!!!

Thank you very much!!!!!! I tried and it worked like magic to me!!! I really appreciate it!!! Thank you very much again!! You wouldn't know how convenient it is for me! (I listen to my favourite stuff in another language (Bill Burr, Jim Jefferies, my Vietnamese Youtube channels, food channels,...) while I work and love be able to quickly rewind the part I want to pay attention to).

PS. By accidence, I discovered that, after you adjust the Youtube volume with your mouse clicking (focusing) on the Youtube video's volume bar, the Rewind shortcut will decrease the volume instead of rewind. Hope you'll fix it soon <3

Have a great day! You deserve it!

ananthuthilakan wrote:
30 Jan 2023, 14:45
the below code will work both for firefox and VLC ,
i made this script long time ago.
Hope this helps

Code: Select all

#SingleInstance,Force

;############################################
; author : discretecourage#0179
;############################################
; How to use (works both with vlc and firefox)
;############################################
; f6 ==> change vlc into borderless
; F7 ==> rewind
; (Alt + Space) or F8 ==> pause 
; F9 ==> forward
; F10 ==> change the position of vlc if you saved the from tray icon
;############################################


SetWorkingDir %A_ScriptDir%
;~ Menu, Tray, Icon, play.ico
Menu,Tray, NoStandard


Menu, SaveCurrentPosID, add, Profile_1, Profile_1
Menu, SaveCurrentPosID, add, Profile_2, Profile_2
Menu, SaveCurrentPosID, add, Profile_3, Profile_3
Menu, SaveCurrentPosID, add, Profile_4, Profile_4
Menu, SaveCurrentPosID, add, Profile_5, Profile_5
Menu, Tray, add, Save Current Position, :SaveCurrentPosID
Menu, SetDefaultPosID, add, Set_1, Set_1
Menu, SetDefaultPosID, add, Set_2, Set_2
Menu, SetDefaultPosID, add, Set_3, Set_3
Menu, SetDefaultPosID, add, Set_4, Set_4
Menu, SetDefaultPosID, add, Set_5, Set_5
Menu, Tray, add, Set Default, :SetDefaultPosID
Menu, Tray, Add,  Restart, Reloadlbl
Menu, Tray, Add,  close, Terminate



IniRead,DefaultPos,VLCPowerTools.ini,Default,DefaultPos,1
Loop,5
IniRead,Pos_%A_Index%,VLCPowerTools.ini,Positons,Pos_%A_Index%,0|0|550|350
Menu, SetDefaultPosID, Check, Set_%DefaultPos%
return

Set_1:
Set_2:
Set_3:
Set_4:
Set_5:
loop,5
Menu, SetDefaultPosID, UnCheck, Set_%A_Index%
Menu, SetDefaultPosID, Check, %A_ThisLabel%
t:=SubStr(A_ThisLabel, 5)
DefaultPos:=t
IniWrite,%t%,VLCPowerTools.ini,Default,DefaultPos
t:=""
return


Profile_1:
Profile_2:
Profile_3:
Profile_4:
Profile_5:
t:=SubStr(A_ThisLabel, 9)
WinGetPos, tX, tY, tW, tH, ahk_exe vlc.exe
Temp:= tX "|" tY "|" tW "|" tH
if (tX!="") {
Pos_%t%:= tX "|" tY "|" tW "|" tH
IniWrite,%Temp%,VLCPowerTools.ini,Positons,Pos_%t%
}
Else
{
MsgBox, 16, VLC Not Found?, Cant get VLC Position from screen
}
return




Reloadlbl:
Reload
return

SaveCurrentPos:
WinGetPos, X, Y, W, H, ahk_exe vlc.exe
Temp:= X "|" Y "|" W "|" H
if (X!="") {

}

Return

Terminate:
ExitApp
return

F10::
DefaultPos:= (DefaultPos=5) ? 1 : DefaultPos+1
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1],strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3],strsplit(Pos_%DefaultPos%,"|")[4]
return

#If !WinActive("ahk_exe vlc.exe")
f6::

ControlSend,Qt5QWindowIcon7,^{h},ahk_exe vlc.exe
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]-1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]-1,strsplit(Pos_%DefaultPos%,"|")[4]
WinSet, Style,  ^0xC40000, ahk_exe vlc.exe
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]+1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]+1,strsplit(Pos_%DefaultPos%,"|")[4]

return
#if

#If WinActive("ahk_exe vlc.exe")
f6::

SendInput, ^h 
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]-1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]-1,strsplit(Pos_%DefaultPos%,"|")[4]
WinSet, Style,  ^0xC40000, ahk_exe vlc.exe
WinMove, ahk_exe vlc.exe,, strsplit(Pos_%DefaultPos%,"|")[1]+1,strsplit(Pos_%DefaultPos%,"|")[2],strsplit(Pos_%DefaultPos%,"|")[3]+1,strsplit(Pos_%DefaultPos%,"|")[4]
return
#if

#If (!WinActive("ahk_exe vlc.exe")) 
!Space::
F8:: ;Set your hotkey to play/pause here
ControlSend, ahk_parent, {Space}, ahk_exe firefox.exe
ControlSend,Qt5QWindowIcon7,{space},ahk_exe vlc.exe ;Send space to VLC player control
return

F9::
ControlSend,Qt5QWindowIcon7,+{Right},ahk_exe vlc.exe ;forward
ControlSend, ahk_parent, {Right}, ahk_exe firefox.exe
return

F7::
ControlSend, ahk_parent, {Left}, ahk_exe firefox.exe
ControlSend,Qt5QWindowIcon7,+{Left},ahk_exe vlc.exe ;backward
return
#if

#If WinActive("ahk_exe vlc.exe")
!Space::
F8::
SendInput, {Space}
return

F9::
SendInput, +{Right}
return

F7::
SendInput, +{Left}
return
#if

*** Thank you for reading. I am not in coding and know almost nothing about professional coding, hope for your patience and deeply appreciate any of your kind helps. My current interest in this awesome AHK is due to that my work is graphical ((architect/CAD) and, to reduce strains, my right hand is better off not leaving the mouse (an MMO mouse that has 12 side keys which I maps a lot of F keys and other keys in) as much as possible. All the best you lovely coders! ***

ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: Control media without focusing on the window

Post by ananthuthilakan » 31 Mar 2023, 05:32

What ever i post on the forum is fully free to use, don't worry about payment or anything.

Also in the mean time i created a fully fledged application , for my own study purpose to take notes from youtube with timestamps , screenshots etc

if you are interested pls checkout my blog https://ananthuthilakan.com search for VlcNotes App ; its also free to download.

Have a nice day

Post Reply

Return to “Ask for Help (v1)”