The Computer Oracle

Keyboard macro to change default audio device

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Sunrise at the Stream

--

Chapters
00:00 Keyboard Macro To Change Default Audio Device
00:41 Accepted Answer Score 5
02:17 Answer 2 Score 1
03:38 Answer 3 Score 2
04:19 Answer 4 Score 1
04:30 Thank you

--

Full question
https://superuser.com/questions/426293/k...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#windows7 #audio #macros

#avk47



ACCEPTED ANSWER

Score 5


Based off of this article.

  1. Download and install AutoHotkey.

  2. Open your sound control panel. This can also be done through running mmsys.cpl through the start menu search or the run dialog.

  3. Note how far down the list your desired options are. In the following image, the HDMI Output is item 1 and the current default Speakers is item 4.

    Screenshot of sound control panel

  4. Modify the following script. F6 and F7 represent the activation key (F6 and F7) (the * means this hotkey applies even when modifiers such as Ctrl are pressed). The {Down #} command indicates how far down the list to go. From your screenshots, you want {Down 3} for Speakers and {Down 4} for Headset. If you add or remove audio devices, or show/hide disabled items, the number will change.

    *F6::
        Run, mmsys.cpl
        WinWait,Sound
        ControlSend,SysListView321,{Down 3}
        ControlClick,&Set Default
        ControlClick,OK
        return
    
    *F7::
        Run, mmsys.cpl
        WinWait,Sound
        ControlSend,SysListView321,{Down 4}
        ControlClick,&Set Default
        ControlClick,OK
        return
    
  5. Run the script. You can set the script to run at startup if you'd like.


The reason I call this a 'hack' is the script actually opens the control panel (a GUI window). Ideally, this could be done through the command line, by specifying the sound device's GUID.

There's apparently a program with a CLI to switch sound devices. You supply the same number in the list, which makes me think it's not much different from the AutoHotkey 'hack' here. In any case, something like AHK would be required to bind it to a hotkey.




ANSWER 2

Score 2


Auto Hotkey script as a toggle using the scroll-lock key.

take out items by disabling them (such as hdmi on your lcd's etc)

I prefer this as I only need to change my sound output if it is set to the incorrect selection. Hitting scroll-lock switches me to the correct one. Very useful if i am already loading a game and realize it is wrong. Most games need to be restarted if you want to change the sound output.

ScrollLock:: 
  toggle:=!toggle ;toggles up and down states. 
  Run, mmsys.cpl 
  WinWait,Sound ; Change "Sound" to the name of the window in your local language 
  if toggle
    ControlSend,SysListView321,{Down 1} ; This number selects the matching audio device in the list, change it accordingly 
  Else
    ControlSend,SysListView321,{Down 2} ; This number selects the matching audio device in the list, change it accordingly 
  ControlClick,&Set Default ; Change "&Set Default" to the name of the button in your local language 
  ControlClick,OK 
return



ANSWER 3

Score 1


The following script allows you to create a shortcut on the desktop that toggles between two devices. The script may need modifying for the correct device names and uses nircmd which needs downloading. A hotkey for the shortcut could be used. Mainly posting here for a record. The icon and script name toggle to reflect the current device. It is possible for them to get out of sync as there is no check(need a command to get the current default device).

' Set the audio device names to use(Speakers and Headphones/PC Headphones. Fixup dir to nircmdc

Const ALL_USERS_DESKTOP = &H19&
Const USER_DESKTOP = &h10&
Const nircmd = "D:\Windows\Commands\Nircmdc"
Const Device1 = "Speakers"
Const Device1Name = "Speakers"
Const Device2 = "PC Headphones"
Const Device2Name = "Headphones"

Set ws = CreateObject("Wscript.Shell")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(USER_DESKTOP)
Set objFolderItem = objFolder.ParseName(Device2Name +".lnk")


if isNull(objFolderItem) or IsEmpty(objFolderItem) or (objFolderItem is Nothing) then
    Set objFolderItem = objFolder.ParseName(Device1Name + ".lnk")
    if isNull(objFolderItem) or IsEmpty(objFolderItem) or (objFolderItem is Nothing) then       
        ' Creates shortcut on desktop to toggle between devices and sets the default to Speakers
        Set oMyShortcut = ws.CreateShortcut(objFolder.Self.Path + "\"+Device1Name+".lnk")
        oMyShortcut.WindowStyle = 0
        OMyShortcut.TargetPath = WScript.ScriptFullName
        'oMyShortCut.Hotkey = "ALT+CTRL+S"
        oMyShortcut.IconLocation = "C:\Windows\System32\mmres.dll, 0"
        oMyShortCut.Save

        ws.run nircmd + " setdefaultsounddevice """+Device1+""" 0", 0
        ws.run nircmd + " setdefaultsounddevice """+Device1+""" 1", 0
        ws.run nircmd + " setdefaultsounddevice """+Device1+""" 2", 0
        msgbox "Desktop link created for """+Device1+""". "+Device1+" set as default!", 0, "Error"
    else
        ' Speaker was set, make headphones
        Set objShellLink = objFolderItem.GetLink
        objShellLink.SetIconLocation "C:\Windows\System32\mmres.dll", 2
        objShellLink.Save()
        objFolderItem.Name = Device2Name

        ws.run nircmd + " setdefaultsounddevice """+Device2+""" 0", 0
        ws.run nircmd + " setdefaultsounddevice """+Device2+""" 1", 0
        ws.run nircmd + " setdefaultsounddevice """+Device2+""" 2", 0

    end if
else
    ' Headphones was set, make speakers
    Set objShellLink = objFolderItem.GetLink    
    objShellLink.SetIconLocation "C:\Windows\System32\mmres.dll", 0
    objShellLink.Save()
    objFolderItem.Name = "Speakers"

    ws.run nircmd + " setdefaultsounddevice """+Device1Name+""" 0", 0
    ws.run nircmd + " setdefaultsounddevice """+Device1Name+""" 1", 0
    ws.run nircmd + " setdefaultsounddevice """+Device1Name+""" 2", 0
end if



ANSWER 4

Score 1


Setting up a batch file to run NirCmd was the easiest way for me, using the setdefaultsounddevice command, ie:

nircmd setdefaultsounddevice "Speaker 1"