Control your android media via your PC media keys

Post your working scripts, libraries and tools for AHK v1.1 and older
Bluscream
Posts: 16
Joined: 10 Jan 2017, 23:12
Contact:

Control your android media via your PC media keys

Post by Bluscream » 31 Mar 2024, 18:41

Requires adb enabled on the device and being either connnected via USB or having wifi adb turned on

Code: Select all

; #include <consoleapps> ; https://github.com/Bluscream/ahk-scripts/blob/master/lib/consoleapps.ahk

; Define the ADB path. Adjust the path according to your ADB installation.
adb_path := "adb"
; Default behavior for volume keys
HandleVolumeKeys := true

; Check for command line switches
Loop, %0%
{
    Parameter := %A_Index%
    if (Parameter = "noVolume")
    {
        HandleVolumeKeys := false
    }
}

; Function to send ADB command
MakeAdbCommand(command) {
    global adb_path
    return adb_path . " shell cmd media_session dispatch " . command
}
SendADBCommand(command) {
    cmd := MakeAdbCommand(command)
    RunWait % cmd, , Min UseErrorLevel
    if (ErrorLevel = "ERROR") {
        MsgBox, % "ADB command failed with exitcode " . ExitCode
    }
}
; SendADBCommand(command) {
;     cmd := MakeAdbCommand(command)
;     ComOBJ := ComObjCreate("WScript.Shell").Exec(cmd)
;     Response := ComOBJ.StdOut.ReadAll()
;     if (ComOBJ.ExitCode != 0) {
;         MsgBox, % "ADB command failed: " . Response
;     }
; }
; SendADBCommand(command) { ; Requires http://www.autohotkey.com/board/topic/96903-simplified-versions-of-seans-stdouttovar/?p=610306
;     cmd := MakeAdbCommand(command)
;     result := StdOutToVar(MakeAdbCommand(cmd))
;     if (InStr(result, "err") || InStr(result, "fail")) {
;         MsgBox, % "ADB command failed:`n`n" . result
;     }
; }

; Hotkeys for media keys
#InstallKeybdHook
#InstallMouseHook
#NoEnv
#SingleInstance force

; Play/Pause
~Media_Play_Pause::SendADBCommand("play-pause")

; Stop
~Media_Stop::SendADBCommand("stop")

; Previous
~Media_Prev::SendADBCommand("previous")

; Next
~Media_Next::SendADBCommand("next")

; Volume Up
~Volume_Up::
if (HandleVolumeKeys)
    SendADBCommand("volume --adj raise")
else
    Send, {Volume_Up}
return
; Volume Down
~Volume_Down::
if (HandleVolumeKeys)
    SendADBCommand("volume --adj lower")
else
    Send, {Volume_Down}
return
; Mute
~Volume_Mute::
if (HandleVolumeKeys)
    SendADBCommand("mute")
else
    Send, {Volume_Mute}
return

; Ensure the script keeps running
return

https://github.com/Bluscream/ahk-scripts/blob/master/scripts/adb%20media%20keys.ahk
https://github.com/Bluscream/ahk-scripts/blob/master/lib/consoleapps.ahk


[Mod action: Moved topic to the v1 section since this is v1 code. The main section is for v2. Please post in the correct section in the future.]

Return to “Scripts and Functions (v1)”