11

Windows 7, Logitech G110 keyboard

2 audio devices (headphones and speakers)

I am hoping to find a quick way to macro a key to switch to headphones, and a key to switch to speakers.

Anyone have a good program or anything of that nature?

EDIT: Using AHK to try and get this task working as hoped is sadly not doing the trick. I have included a screenshot of my audio set, as well as a screenshot of my script as it sits now. Sound setup Script

I have also tried to uncheck "show disabled devices" and modified the script to be Down 1, and Up 1 respectively. It will work for one device, but it refuses to switch to the secondary.

4
  • 3
    Autohotkey hacks: this article or the second post here. If someone has enough time to write one of those up into a full answer, feel free.
    – Bob
    May 19, 2012 at 5:32
  • Followed the instructions in those 2 links, still no luck sadly, AHK doesnt seem to like my audio selection choices, and constantly defaults to my Speakers instead of switching between headphones and speakers
    – user76211
    May 19, 2012 at 5:57
  • Well, what are your choices? How about a screenshot, or at least a list?
    – Bob
    May 19, 2012 at 6:06
  • @Bob - Sorry about that, I have edited the post with the info requested.
    – user76211
    May 19, 2012 at 18:40

10 Answers 10

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.

2
  • You sir, are fantastic. Thank you for the assistance. It is now a functional script! Flagged as the answer, and upvoted. Much appreciated!
    – user76211
    May 19, 2012 at 21:23
  • If you just want to toggle between two outputs, here is an updated script to do that.
    – MBraedley
    Nov 22, 2012 at 1:10
9

Audioswitch (https://github.com/sirWest/AudioSwitch) allows you to quickly change default audio device (with a click on a tray icon), has hotkey support, shows volume levels on click, and allows custom colours for the tray icon to show which device is in use. Plus, it's open source. So if you don't like it, fork off ... (to your heart's delight).

A second answer is Volume2 (https://github.com/irzyxa/Volume2), another free (but not open source) piece of software. You can set hotkeys to change devices. (You can also, as has been pointed out in another answer, change by othermeans, but this was not what OP asked so I didn't get into it.) It has now replaced, for me, AudioSwitch, mostly because it allows more flexible volume adjustments (which again wasn't OP's question).

A third answer is Audio Switcher. http://audioswit.ch/er. It also allows changing on click, and hotkeys. It does not allow volume changing with the mouse, at least in version 1.

1
  • 1
    Thank you for Volume2, this not only allows me to switch devices, but solves a few of my other issues as well. Works well with Microsoft key remapping via the new MS Power Toys to boot.
    – Marius
    Jan 14, 2021 at 21:12
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
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
1

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

nircmd setdefaultsounddevice "Speaker 1"

1

I am using this simple autohotkey script with nircmd utility. The only problem - I cant figure out how to switch between more than 2 audio devices with just 1 key.

F1::
    if (toggle1 := !toggle1) 
    { 
        run, c:\windows\system32\displayswitch.exe /internal 
    } 
    else 
    { 
       run, c:\windows\system32\displayswitch.exe /external 
    }

F2::
    if (toggle2 := !toggle2) 
    { 
        run, nircmd.exe setdefaultsounddevice "tv"
    } 
    else 
    { 
       run, nircmd.exe setdefaultsounddevice "headphones"
    }
1

I want to complete the answer about Volume2. This program has three places where you can change the default audio device:

  1. Hotkeys

    Volume2 Set Default Device Hotkeys

  2. Tray icon popup menu

    Volume2 Set Default Device TrayMenu

  3. Tray icon mouse click (left, double left, middle)

3
1

The solution with installation of Volume2 was the easiest and fastest for me.

  1. Download and install Volume2

  2. Open it, Go in Keyboard section

  3. Delete all the entry by selecting them and pressing the button "Delete"

  4. Add a new entry by pressing button "Add" and Add "Set next default device"

  5. Put a key combination Ctrl + Shift + 1

    image description here

  6. Press the "MR" button on you Logitech keyboard, press an extra Macro Key in my example "G1", Press Ctrl + Shift + 1, and press again the "MR" button.

You are done.

In the future when you press G1 button on your keyboard (or shortcut Ctrl + Shift + 1) it will switch to the next audio device.

I have 3 audio devices, so I just press once or twice to get to the right one!

Thanks to @Alexandr Irza for the great tip!

1
0

This is an old question, but I ended up spending some time on it to get what I wanted. I modified Bob's answer to also change the active microphone; useful for me since I have wireless USB headphones that are both an audio device and a listening device, but I just recently got Windows 10 where it can be useful to have my webcam listening for "Hey, Cortana...".

For each function, on the second SysListView321 line, enter the index of the microphone you want to use, similarly to the audio device. I switched the keys to numkeys, which is just my own preference - you may have your own choice.

*Numpad0::
    Run, mmsys.cpl
    WinWait,Sound
    ControlSend,SysListView321,{Down 2}
    ControlClick,&Set Default
    ControlSend,SysTabControl321,{Right 1}
    ControlSend,SysListView321,{Down 2}
    ControlClick,&Set Default
    ControlClick,OK
    return

*NumpadDot::
    Run, mmsys.cpl
    WinWait,Sound
    ControlSend,SysListView321,{Down 1}
    ControlClick,&Set Default
    ControlSend,SysTabControl321,{Right 1}
    ControlSend,SysListView321,{Down 1}
    ControlClick,&Set Default
    ControlClick,OK
    return
0

In new update of Windows 11 (build 22621.2715) you can press Ctrl+Win+V to open sound output and volume mixer

You must log in to answer this question.