Sunday, March 31, 2013

Tip: Switch Audio Devices Easily with AutoHotKey

Many people (especially audiophiles and audio engineers) have multiple sound outputs from their computers. Furthermore, they might even have multiple inputs. It is a little tedious to go to the start menu, find sound, switch the device, close out, and then repeat it every few minutes or so. Between RCA, MIDI, and USB, chances are that you will have more than one set of speakers or music makers connected to your computer at any given time. Use AutoHotKey and a little custom script to make switching fast and seamless.

Difficulty: Medium
Time: 10-20 minutes
Tools: AutoHotKey


Okay, so you've downloaded and installed AutoHotKey (or you will now), so now what? You may have noticed a green icon in your notification bar as highlighted in the snippet to the left. Right click on it and choose open.

You will see a couple lines of code, but nothing that matters yet. Click on "File", and choose "Edit Script." This is where we will add our hotkey to change audio devices quickly. The document will have some lines above and below that begin with semicolons, in between these should be some example code. Erase this code and leave some empty lines between the semicolons to add your code. This is the code you need, I will tell which is customizable:

F6::
    Run, mmsys.cpl
    WinWait,Sound
    ControlSend,SysListView321,{Down 2}
    ControlClick,&Set Default
    ControlClick,OK
    return
F7::
    Run, mmsys.cpl
    WinWait,Sound
    ControlSend,SysListView321,{Down 5}
    ControlClick,&Set Default
    ControlClick,OK
    return


Copy and paste the code as is and we will edit the red and blue highlighted code. The red highlighted code represents the key you will press to activate the code. If you want a combination of keys, you could do something like #+a:: to have WindowsKey+Shift+A, for example. It is simplest, however, if you use single keys that aren't often used. As you can see I chose F6 and F7. Next notice the blue text, this signifies which audio device is selected when you go into your sound properties (or mmsys.cpl).


{Down 2} simply represents the second audio device in my list, and {down 5} the fifth. To make sure you select the proper device, right click on a device and click "show disabled devices" and "show disconnected devices." This will show your true device ordering. If you are trying to select the first audio device I do not know if you need to put {down 1} or simply {down}, so you will have to experiment. If you want more than two hotkeys, simply copy the code and change the hotkey and audio device selector.

The code itself is pretty self-explanatory. First, it runs mmsys.cpl (sound properties), then it scans the list(Run, mmsys.cpl | WinWait,Sound | ControlSend,SysListView321,{Down 2}). Once it completes that it performs the actions you set for it to complete. Once it selects the device, it sets the default (ControlClick,&Set Default), clicks OK(ControlClick,OK), and closes(return). Seamless, easy, not obtrusive.

I hope this works well for everyone who uses it. I love it. There are programs such as "Sound Switch" that do a similar thing without custom coding, but they are buggy and do not work for everyone consistently. This is a sure-fire method if done correctly. It is great for switching from external speakers to a headset or testing sound files on different quality speakers, I find.

Some Notes:
-You may have to right-click on the green icon in the notification bar and click 'reload scripts.'
-Set AutoHotKey to autostart on computer startup
-If your mouse is over sound properties when it shows up, the code with pause execution. It is a minor annoyance but you will eventually get used to where it shows up.

Enjoy!

1 comment:

  1. Here's a version that only runs 1 ahk script, it "toggles" the devices instead.

    F7::
    {
    toggle:=!toggle ;toggles up and down states.

    if toggle
    {
    Run, mmsys.cpl
    WinWait,Sound
    ControlSend,SysListView321,{Down 2}
    ControlClick,&Set Default
    ControlClick,OK
    return
    }
    if !toggle
    {
    Run, mmsys.cpl
    WinWait,Sound
    ControlSend,SysListView321,{Down 3}
    ControlClick,&Set Default
    ControlClick,OK
    return
    }
    }
    return

    ReplyDelete