5

I have two primary audio outputs on my machine: The speakers and a USB headset. Currently, in Windows 7 Professional x64, I type "sound" in the Start search menu to open up this dialog:

Windows 7 Sound Dialog

I only care about the top two options for the purposes of this question. I would like to know if there's a way in AutoHotKey to switch from "Speakers (4- Sennheiser USB Headset)" to "Speakers (VIA High Definition Audio)" so I can avoid having to open this dialog every time I wish to switch. Thanks!

1

3 Answers 3

4

I found myself an answer thanks to iglvzx's help pointing out the correct terminology to use!

; Toggles default audio device in Control Panel (switches between headset and speakers).
ScrollLock:: 
  toggle:=!toggle ;toggles up and down states. 
  Run, mmsys.cpl 
WinWait,Sound
if toggle
  ControlSend,SysListView321,{Down 1}
Else
  ControlSend,SysListView321,{Down 2}
ControlClick,&Set Default
ControlClick,OK 
return

ScrollLock is the key that I press. toggle is a variable that AutoHotKey holds onto (as far as I can tell) which the conditional down below uses. mmsys.cpl is the window pictured above (type it in Run to see for yourself!). The rest of the commands deal with handling the UI, from selecting the correct part of the window to simulating "clicks" to clicking "Set Default" (must match your current language) to clicking OK (ditto).

When this is run you will notice the window physically pop up for a half-second as it physically runs through and selects everything. While I would prefer a windowless option this works fine, as focus is returned back to the correct window (even fullscreen games) after it runs.

1
  • 1
    Cool! I was going to come back to this, but you already figured it out. Glad I could point you in the right direction. :)
    – iglvzx
    Commented Jul 9, 2012 at 1:08
0

I just tweaked a script by "aafe". It works with several outputs and cycles trough them:

; Audio Output Default cycle
ScrollLock:: 
    Run,mmsys.cpl
    WinWait,YOURSOUNDWINDOWNAME
    loop,20 ; Exits loop after 20 runs. Good if there's just one output.
    {
        ControlSend,SysListView321,{Down}
        ControlGet,isEnabled,Enabled,,&YOURSETDEFAULTBUTTONNAME
        if(!isEnabled)
        {
            break
        }
    }
    ControlSend,SysListView321,{Down}
    ControlGet, isEnabled, Enabled,, &YOURSETDEFAULTBUTTONNAME
    if(!isEnabled)
    {
        ControlSend,SysListView321,{Home}
    }
    ControlClick,&YOURSETDEFAULTBUTTONNAME
    ControlClick,OK
    WinWaitClose
    SoundPlay, *-1
return

Change the text in all caps to the name of the sound window and the name of the "Set Default" button. Keep the "!&"

0

Here is a solution that loops through all audio devices and will exclude ones you don't want, like S/PDIF or Digital Output using Windows Key + Shift + W.

#+w::
IfWinExist Sound
{
    WinKill Sound
}
Run rundll32.exe shell32.dll`,Control_RunDLL mmsys.cpl`,`,0 

WinWait,Sound 
IfWinNotActive,Sound WinActivate,Sound 
WinWaitActive,Sound

ControlSend,SysListView321,{Home}  
x := 1
totalDevicesCount := 0
isSecondLoop := false
isEnabled := false
;    ControlGet, isEnabled, Enabled,,&Set Default 
ControlGet, totalDevicesCount, List, Count, SysListView321 
; isEnabled AND 
StringCaseSense Off
loop 
{ 
    ControlGet, Current, List, Selected, SysListView321
    if(InStr(Current, "default device") OR x > totalDevicesCount)
    {
        break
    }
    x := x + 1
    ControlSend,SysListView321,{Down}  
} 

if (x >= totalDevicesCount) 
{
    ControlSend,SysListView321,{Home}
    x := 1 
} 

loop 
{ 
    ControlGet, Current, List, Selected, SysListView321
    ControlGet, isEnabled, Enabled,,&Set Default 
    ; msgbox %x% %isEnabled% %Current%

    ; exclude output option with the name specified
    if(InStr(Current, "Realtek Digital Output"))
    {
        isEnabled := false
    }

    if (x >= totalDevicesCount AND not isEnabled)
    {
        ControlSend,SysListView321,{Home}
        x := 1
        isSecondLoop := true
        ; msgbox breaking now %x% %totalDevicesCount% %isEnabled% %isSecondLoop%
        break
    }
    if (x > totalDevicesCount OR isEnabled)
    {
        ; msgbox breaking now %x% %totalDevicesCount% %isEnabled% %isSecondLoop%
        break
    }
    if (x >= totalDevicesCount AND isSecondLoop)
    {
        ; infinite loop breaker if no devices avaliable
        ; msgbox breaking sec %x% %totalDevicesCount% %isEnabled% %isSecondLoop%
        break
    }

    ControlSend,SysListView321,{Down}
    x := x + 1
} 


;ControlGet, Current, List, Selected, SysListView321 
;msgbox %Current% 
Sleep 100
ControlClick,&Set Default 
ControlClick,OK 
;    SoundPlay *48 
WinKill Sound

return

I modified this script from posted by dan112123 on this forum: http://www.autohotkey.com/board/topic/2306-changing-default-audio-device/page-4

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .