Keyboard macro to change default audio device

5

2

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.

user76211

Posted 2012-05-19T05:13:05.550

Reputation:

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 – 2012-05-19T05:32:39.597

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 – None – 2012-05-19T05:57:24.777

Well, what are your choices? How about a screenshot, or at least a list? – Bob – 2012-05-19T06:06:15.530

@Bob - Sorry about that, I have edited the post with the info requested. – None – 2012-05-19T18:40:22.613

Answers

4

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.

Bob

Posted 2012-05-19T05:13:05.550

Reputation: 51 526

You sir, are fantastic. Thank you for the assistance. It is now a functional script! Flagged as the answer, and upvoted.

Much appreciated! – None – 2012-05-19T21:23:14.383

If you just want to toggle between two outputs, here is an updated script to do that.

– MBraedley – 2012-11-22T01:10:37.847

6

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://irzyxa.wordpress.com/), 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.

Jon

Posted 2012-05-19T05:13:05.550

Reputation: 235

1

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

Ozhound

Posted 2012-05-19T05:13:05.550

Reputation: 11

0

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"
    }

monstro

Posted 2012-05-19T05:13:05.550

Reputation: 119

0

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)

Alexandr Irza

Posted 2012-05-19T05:13:05.550

Reputation: 1

2

This is really a comment and not an answer to the original question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. Please read Why do I need 50 reputation to comment? What can I do instead?

– DavidPostill – 2016-05-08T11:03:29.653

This doesn't answer OP's question, but for completeness I added to my answer, in which I mentioned Volume2, that it has other ways of changing the default audio device. – Jon – 2016-09-03T20:55:30.340

0

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

user14448

Posted 2012-05-19T05:13:05.550

Reputation: 29

0

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

nircmd setdefaultsounddevice "Speaker 1"

NightShovel

Posted 2012-05-19T05:13:05.550

Reputation: 551

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

Katana314

Posted 2012-05-19T05:13:05.550

Reputation: 233