Skip to content

Instantly share code, notes, and snippets.

@Iaotle
Last active August 19, 2025 09:56
Show Gist options
  • Select an option

  • Save Iaotle/cd3c90cee641b021d43dab8618d5db54 to your computer and use it in GitHub Desktop.

Select an option

Save Iaotle/cd3c90cee641b021d43dab8618d5db54 to your computer and use it in GitHub Desktop.
AutoHotkey for managing bluetooth audio along with a speaker-headset pair.
; Requires VA >= 2.3 https://www.autohotkey.com/board/topic/21984-vista-audio-control-functions/
; Requires https://bluetoothinstaller.com/bluetooth-command-line-tools/BluetoothCLTools-1.2.0.56.exe
#SingleInstance
#Include %A_ScriptDir%\VA.ahk
SendMode Input
; Shogun 2 bind for borderless:
; F12 & MButton::
; WinSet, Style, -0xC40000, A
; WinMove, A, , 0, 0, %A_ScreenWidth%, %A_ScreenHeight%
; Return
; Some logic to connect/disconnect to the BT headset, requires bluetooth command line tools
; gist taken from https://gist.github.com/joshjm/69dcef304386e928c10c9534c73c3a04
#c::
; connect to the XM4s using BluetoothDevicePairing.exe
RunWait, "C:\Users\Vadim\Desktop\BluetoothDevicePairing.exe" pair-by-mac --mac "88:C9:E8:67:1E:CF" --type "Bluetooth"
run nircmd setdefaultsounddevice "Headphones"
RunWait, "C:\Program Files (x86)\Bluetooth Command Line Tools\bin\btcom.exe" -r -b "88:C9:E8:67:1E:CF" -s111e ; don't use hands-free profile, audio quality sucks on it
; make sure we're actually playing sound via them as well
run nircmd setdefaultsounddevice "Headphones"
sleep, 3000
if !RegExMatch(current_device := getCurrentDevice(), "i)^Headphones \((\d+-\s*)?WH-1000XM4\)$")
{
run nircmd setdefaultsounddevice "Headphones"
}
Return
#+c::
; switch to speakers
run nircmd setdefaultsounddevice "Speakers"
; disconnect from XM4s using BluetoothDevicePairing.exe
Run, "C:\Users\Vadim\Desktop\BluetoothDevicePairing.exe" disconnect-bluetooth-audio-device-by-mac --mac "88:C9:E8:67:1E:CF" --type "Bluetooth"
Return
; My thumb button on g502 is bound to F24
F24 & WheelUp::send {Volume_Up}
F24 & WheelDown::send {Volume_Down}
; Requires VA >= 2.3
getCurrentDevice()
{
return va_getdevicename(va_getdevice("playback"))
}
F24 & MButton::
; msgbox % getCurrentDevice()
current_device := getCurrentDevice() ; Sennheiser HD650 (Realtek USB Audio) || Speakers (5- Logitech USB Speaker) || Headphones (3- WH-1000XM4)
if RegExMatch(current_device, "i)^Sennheiser HD650 \((\d+-\s*)?Realtek USB Audio\)$")
{
; wired headphones. we want to switch to the wireless headset if possible:
run nircmd setdefaultsounddevice "Headphones"
; check if it worked
sleep, 100 ; need to wait for the switcher to update the default device
current_device := getCurrentDevice()
if !RegExMatch(current_device, "i)^Headphones \((\d+-\s*)?WH-1000XM4\)$")
{
; didn't work, switch to speakers
run nircmd setdefaultsounddevice "Speakers"
}
}
else if RegExMatch(current_device, "i)^Speakers \((\d+-\s*)?Logitech USB Speaker\)$")
{
; speakers. same as above:
run nircmd setdefaultsounddevice "Headphones"
; check if it worked
sleep, 100 ; need to wait for the switcher to update the default device
current_device := getCurrentDevice()
if !RegExMatch(current_device, "i)^Headphones \((\d+-\s*)?WH-1000XM4\)$")
{
; didn't work, switch to wired headphones
run nircmd setdefaultsounddevice "Sennheiser HD650"
}
}
else if RegExMatch(current_device, "i)^Headphones \((\d+-\s*)?WH-1000XM4\)$")
{
; bluetooth headphones. want to switch to speakers 90% of the time
run nircmd setdefaultsounddevice "Speakers"
return
}
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment