Hotkey toggle the default audio output device

So I got this beautiful Sony TV and hooked it up to my work computer via HDMI. It’s great for watching movies and SageTV Client from my computer and every once in a while I even use it as an extended desktop. I also have a set of speakers on my desk that I use whenever I’m working and the TV is turned off. Whenever I enable the Sony as an extended monitor, it automatically sets the AMD HDMI Output audio device as my default audio device, however when I turn the TV off it doesn’t set my speakers back as my default audio output device. This is a problem because the TV is turned off a lot of the time and if the default audio device is still the TV, I won’t be hearing any voice mails while working. To fix this in the past, I’d have to go to mmsys.cpl and set my speakers back as my default audio output device. I finally sat down and tweaked an AutoHotkey script to do this for me. The script simply toggles between both Audio Output devices with a hotkey, so I can easily switch back and forth.

Here’s how:

[the problem]

When I extend my monitor to include the Sony TV, the AMD HDMI Output device is set as default.

But I want to switch back to the Speakers Output device without having to go to mmsys.cpl.

[the solution]

[step 1] Get AutoHotkey (if you don’t already have it). It’s free.

[step 2] Create a new text file anywhere you like and name it audio.ahk (make sure to change the extension from txt to ahk).

[step 3] Paste the following text into that file. Then Save and Close the file.

—————start copy below this line——————-

; This script will toggle your default Windows Audio Output device if you have 2 enabled audio devices.
; For more audio devices, contact me at https://davidvielmetter.com/contact/
; This script maps to Win+A hot key combo once activated!
#a::
Run, mmsys.cpl
WinWait,Sound
ControlSend,SysListView321,{Up 2}
ControlGet, isEnabled, Enabled,,&Set Default
if(!isEnabled)
{
  ControlSend,SysListView321,{Down}
}
ControlClick,&Set Default
ControlClick,OK
return

—————stop copy above this line——————-

[step 4] Double Click the file or right-click it and and select Run Script. NOTE: The A icon that should appear in your notification area. This indicates the script is running and awaiting your key presses.

[step 5] Type Win+a (the Windows key and the a key at the same time) to toggle between your default output audio devices.

That’s it.

Comments

10 responses to “Hotkey toggle the default audio output device”

  1. svenne Avatar
    svenne

    that works like a charm, solved my similar problem immediately – swithcing between speakers and wireless headset. Thanks.

  2. Rob Avatar
    Rob

    Well, it seems like this is the perfect solution for me to toggle between normal and bluetooth speakers. But when I hit Win + a, only the audio screen appears listing all audio devices, and nothing else happens, so it seems to me that it only runs the first two lines of the script:
    #a::
    Run, mmsys.cpl

    I’m I missing something here? I’m running Win7 Ultima 64 bit.
    Btw: It has to switch between the first two devices.

    Ideas anyone?

    Thanks.

    1. David Vielmetter Avatar

      Hmm, not sure what the problem you’re having is. When you run mmsys.cpl, how many devices do you see there?

      Also, is your bluetooth audio device listed in this dialog?

      This script will simply toggle through all of the devices in the list of devices in mmsys.cpl when you press Win+a.

      For troubleshooting, you could remove this line:

      ControlClick,OK

      from the script, reload it in a-h-k then with the mmsys.cpl dialog open hit Win+a and see what happens. It should toggle through the devices there setting the next one as the default.

      Hope it helps.
      David

  3. Rob Avatar
    Rob

    Can I send you an email with some screenshots? You could then see my setup. The devices are listed in the dialog, like I said, and it should toggle between the first one listed (Realtek) and the second one listed (Logitech bluetooth), but it doesnt toggle.

    Thanks.

    1. David Vielmetter Avatar

      sure, send to vielmetter [at] hotmail.com

    2. David Vielmetter Avatar

      Hi Rob,

      Got your email. Looks like you’ve got 4 devices there with one being disabled. I saw your script and am not 100% why it does not work as you expect…but it is different than my script for sure. I know my script works on my system with two audio devices. Please make sure that you’ve got the mmsys.cpl dialog closed when you run AHK and your key combo. When the dialog is open, the script gets confused in my testing. Here’s a more commented version of my script that might help you figure out where your errors might be:

      ; Toggles Audio Output
      #a:: ;***Listens for WIN+a key***
      Run, mmsys.cpl ;***Opens mmsys.cpl***
      WinWait,Sound ;***Wait a second***
      ControlSend,SysListView321,{Up 2} ;***Using the keyboard, hit the UP button 2 times – this makes sure we’re highlighting the TOP item of the 2 items that are in this list – Rob, you could try changing this number to 4 since you have 4 devices***
      ControlGet, isEnabled, Enabled,,&Set Default ;***Get the status of this device (check to see if it is the default device)***
      if(!isEnabled) ;***if the top device is the default***
      {
      ControlSend,SysListView321,{Down} ;***keyboard press down 1 time selecting the next device***
      }
      ControlClick,&Set Default ;***set the current highlighted device as default (note: this is also our ELSE statement because it executes regardless of the if statement. )***
      ControlClick,OK ;***close the mmsys.cpl dialog***
      return

      Try playing with the UP and Down values. Increase and decrease the values until you get the script to do what you want.

      Hope it helps,
      David

  4. Rob Avatar
    Rob

    Nothing changed, but I managed to do it through a macro within my keyboard software, although I would love to see why it isn’t working.

    Thanks anyway.

  5. Jon Christie Avatar

    Thank you So much for this! I am constantly switching between speakers and my headphones which are plugged into my Blue Yeti Mic for direct monitory of my commentary!

    I’ll definitely give you credit if I make a little video tutorial about this!

    Thanks again!
    Jon

  6. Jannis Avatar

    Awesome. I (re-)discovered AutoHotkey just a week ago, and now this gem pops up to magically solve a simple task that Windows still hides bafflingly deep away. Thank you & rock on.

  7. JC Avatar
    JC

    Hey there, you said to contact you if we needed help on HOW TO have 2 enabled audio devices….. I now have an HDTV that I use to watch from my computer as well….I read your solution for the HOTFIX TOGGLE THE WINDOWS AUDIO DEVICES (in my case, like yours too, auto switch to HDMI when using TV from PC & auto switch back to Speakers when using PC)…but to do that, you said we NEED to have 2 audio devices already enabled….so, please tell me how to?
    How to have 2 enabled audio devices?

    Also, that script for the hotfix, is a default that can be used NO matter what audio device, the brand is (Sony, etc) right?

    Please, help me on those 2, so I can start on your solution for me as well.

    Thanks,

    JC.