audiooutputautohotkeynircmd

AutoHotKey v2: changing audio output (with NirCmd)


Say I want the hotkeys "Winkey + F1", "Winkey + F2" and "Winkey + F3" (so that you only need one hand) to switch between headphones, speakers and monitor audio output. I'd also like headphones to be default on startup.

The answer will be posted below!


Solution

    1. If you haven't already, download AutoHotKey version 2 from here (exe) or here (zip).

    2. Download and install NirCmd from here (x64) or here. Copy the path to nircmd.exe.

    3. Create an .ahk file (e.g. ChangeAudioOutput_NirCmd.ahk) with the following content, while making sure that in the code at line 21 you change C:\your\path\to\nircmd.exe to where you installed nircmd.exe:

    #Requires AutoHotkey v2.0
    ; ======================================================================================================================
    ; Everything until the first `return` autoruns
    
    ; ======================================================================================================================
    ; Function definition. Changes Audio Output Device to `device`
    ; ======================================================================================================================
    ChangeAudioOutput(device, show_msg_box:=true)
    {
        symbols := Map("Headphones", "🎧", "Speakers", "🔊", "Display", "🖥️")  ; Python dict-like object, callable by `Val := Array[Key]`
        if (show_msg_box)
            symbol := symbols[device]
            {
                ; Show a message box
                MsgBox(
                    ; MsgBox's message in the box
                    "Selected device: " symbols[device] device,
                    ; MsgBox's title/heading
                    "Audio Output Device changed",  
                    ; time after which the MsgBox will close
                    "T0.3"
                    )
            }
        Run("C:\your\path\to\nircmd.exe setdefaultsounddevice " device)  ; change device using nircmd
    }
    
    ; set Headphones as default device for startup
    ChangeAudioOutput("Headphones", false)  ; false -> don't show message box at startup
    return  ; Everything above this `return` autoruns
    ; ======================================================================================================================
    
    
    
    ; ======================================================================================================================
    ; Audio Output Device Switch to Headphones/Speakers/Monitor
    ; ======================================================================================================================
    ; Audio Output Device Switch to Headphones
    #F1::  ; Windows key + F1
    {
        ChangeAudioOutput("Headphones")
    }
    
    ; Audio Output Device Switch to Speakers
    #F2::  ; Windows key + F2
    {
        ChangeAudioOutput("Speakers")
    }
    
    ; Audio Output Device Switch to Display/Monitor
    #F3::  ; Windows key + F3
    {
        ChangeAudioOutput("Monitor")
    }
    
    1. Make sure your desired audio devices are called exactly "Headphones" and "Speakers" in the Sound Control Panel. To get there:

      • Control Panel -> Hardware and Sound -> Sound (or: Winkey + R -> type "control mmsys.cpl sounds" without quotes, press Enter). Then if they are not exactly named "Headphones", "Speakers" and "Monitor":
      • Rename your desired headphones and speakers devices by right-clicking them and then clicking 'Properties' Note that you could of course also rename the names for the devices in the code.
    2. In order for this to work also after you restart your PC, do the following:

      • Make shortcut of the .ahk file
      • Move the shortcut to Windows' startup folder (Winkey+R -> type "shell:startup" w/o quotes, Enter)

    All done!