I have three endpoints I regularly switch between. Sometimes the USB device isn't plugged in, so all the techniques that depend on the device being in a particular position in the list ended up being unreliable when devices present changed.
Here's another take at it keying off a part of the endpoint identifier and based on all the great work in this post, plus a moving the window off-screen and a traytip showing activated device
Code:
ActivateSoundDeviceEndpoint(DeviceName) {
; close sound panel if it's open
IfWinExist Sound ahk_class #32770
WinClose
; open sound panel
Run rundll32.exe shell32.dll`,Control_RunDLL mmsys.cpl`,`,0 ; similar to Run mmsys.cpl
WinWait Sound ahk_class #32770
; arrow-key movement fails if pointer is over window; get window out of the way
WinMove, 0,8000
; get, then iterate over list of sound device endpoints
ControlGet, len, List, Count, SysListView321
Loop %len%
{
; select first or next endpoint
ControlSend, SysListView321, {Down}
; get endpoint info
ControlGet, Device_Info, List, Selected, SysListView321
; endpoint contans our DeviceName?
IfInString, Device_Info, %DeviceName%
{
; extract endpoint title and device it's on into MatchedTitle and MatchedDevice
RegExMatch(Device_Info, "(?P<Title>.+)\t(?P<Device>.+)\t", Matched)
; set this endpoint as system default (activate it)
ControlClick, &Set Default
ControlClick, OK
WinWaitClose
; play a sound on the newly activated device
SoundPlay, *64 ; Asterisk (info)
; show a tray tip with the new endpoint
TrayTip %MatchedTitle%, %MatchedDevice% is now active, 2, 17 ; 2 seconds, info icon (1) without sound (+16)
SetTimer, RemoveTrayTip, 2000 ; TrayTip durations under 10 seconds don't work, remove ourselves after 2 second timer
; stop iterating
Break
}
}
return
}
RemoveTrayTip() {
SetTimer, RemoveTrayTip, Off
TrayTip ; without parameters, removes displayed traytip
}
In my case, I wanted to bind to a single key, varying which sound endpoint activates by the number of times I press that key in succession.
ins0mniaque's PatternHotKey provided the mechanism
Code:
; Activate sound device endpoint
; '.' (sinle short press) activates Bluetooth headset
; '..' (double short press) activates Speakers
; '...' (triple short press) activates USB headset
ScrollLock::PatternHotKey(".->ActivateSoundDeviceEndpoint(Bluetooth)"
, "..->ActivateSoundDeviceEndpoint(Klipsh)"
, "...->ActivateSoundDeviceEndpoint(Logitech)")