toggle 3 audio devices

Get help with using AutoHotkey and its commands and hotkeys
Xonar
Posts: 4
Joined: 11 Oct 2018, 15:46

toggle 3 audio devices

11 Oct 2018, 16:16

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
Noesis
Posts: 299
Joined: 26 Apr 2014, 07:57

Re: toggle 3 audio devices

12 Oct 2018, 01:07

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
Xonar
Posts: 4
Joined: 11 Oct 2018, 15:46

Re: toggle 3 audio devices

12 Oct 2018, 02:56

Thank you so much Noesis!
That was spot on, worked like a charm!

Best regards

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot] and 146 guests