Page 1 of 1

toggle 3 audio devices

Posted: 11 Oct 2018, 16:16
by Xonar
Hello!
First post...
Found this forum when looking for something to switch between audio devices using keyboard.
Saw this script that works perfectly but I can't for the life of me figure out how to add a third device?
If anyone could guide me it would be much appreciated.
Using Win 7 x64
AutoHotkey 1.1.30.00
NirCmd v2.81

Best regards

The script looks like this:

Code: Select all

#Persistent			; This keeps the script running permanently.
#SingleInstance		; Only allows one instance of the script to run.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Win+A to change Audio Playback Device
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#a::
	toggle:=!toggle ; This toggles the variable between true/false
	if toggle
	{
		Run nircmd setdefaultsounddevice "Speakers"
		soundToggleBox("Speakers")
	}
	else
	{
		Run nircmd setdefaultsounddevice "Headphones"
		soundToggleBox("Headphones")
	}
Return

; Display sound toggle GUI
soundToggleBox(Device)
{
	IfWinExist, soundToggleWin
	{
		Gui, destroy
	}
	
	Gui, +ToolWindow -Caption +0x400000 +alwaysontop
	Gui, Add, text, x35 y8, Default sound: %Device%
	SysGet, screenx, 0
	SysGet, screeny, 1
	xpos:=screenx-275
	ypos:=screeny-100
	Gui, Show, NoActivate x%xpos% y%ypos% h30 w200, soundToggleWin
	
	SetTimer,soundToggleClose, 2000
}
soundToggleClose:
    SetTimer,soundToggleClose, off
    Gui, destroy
Return

Re: toggle 3 audio devices

Posted: 12 Oct 2018, 01:07
by Noesis
You need to allow for a third state, with the toggle variable and check for it.
Something like:

Code: Select all

#a::
	toggle += 1 ; This increments toggle state (so values after execution of this line will be one of 0,1,2)
	if (toggle = 1)
	{
		Run nircmd setdefaultsounddevice "Speakers"
		soundToggleBox("Speakers")
	}
	else if (toggle = 2)
	{
		Run nircmd setdefaultsounddevice "Other Speakers"
		soundToggleBox("Other Speakers")
		toggle :=  -1 ; set to -1 so on next run it will end up being 0
	}
	else
	{
		Run nircmd setdefaultsounddevice "Headphones"
		soundToggleBox("Headphones")
	}
Return

Re: toggle 3 audio devices

Posted: 12 Oct 2018, 02:56
by Xonar
Thank you so much Noesis!
That was spot on, worked like a charm!

Best regards