Close

Controlling speakers with AHK SoundGet, SoundSet

A project log for RemuterMCS

Remote control for muting and unmuting the microphone, camera, and speakers

WJCarpenterWJCarpenter 04/16/2021 at 23:350 Comments

AutoHotKey includes some built-in commands for controlling audio devices on a PC. Although there are several device and control types, I could only get it to work with "master" speakers. There's a lot I don't understand about how Windows thinks about audio devices, but using "master" for the speakers is OK with me.

The AHK SoundSet command will let you explicitly turn speakers on or off, and it also will let you toggle things. Even though I have disparaged hotkey toggles, I found it convenient to use it for implementing speaker control in this AHK function. SoundGet returns either "On" or "Off" for the state of muting of the speakers. To unmute the speakers, we want to "turn off the mute function".

; Control-Shift-F11 mute speakers
^+F11::
  SpeakerControl("On")
return
;;;;;;;;;;;;;;;;;;;
; Control-Shift-F12 unmute speakers
^+F12::
  SpeakerControl("Off")
return

SpeakerControl(StateWanted)
{
  ; mute state "On" means the speakers are muted
  ; mute state "Off" means the speakers are not muted
  SoundGet, MUTE_STATE ,,MUTE
  if (StateWanted != MUTE_STATE)
  {
    ; this toggles mute state, so we only do it if appropriate
    SoundSet, +1, ,MUTE
  }
}

Discussions