Slava
Slava

Reputation: 6650

Switch between three (3) audio devices using autohotkey (AHK)

just found out about AHK. I have three audio devices (Speakers, Headphones, TV)

enter image description here

Cant figure out the AHK syntax to switch between them with one key, lets say F2. This tiny script toggles between two devices, but I need to switch between three.

F2::
if (toggle := !toggle) 
{ 
    run, nircmd.exe setdefaultsounddevice "tv"
} 
else 
{ 
    run, nircmd.exe setdefaultsounddevice "headphones"
}
return

So I was thinking about something like this, where device is a variable to hold audio device name:

device = "Speakers"
F2::
if (device = "Speakers") device = "TV"
else if (device = "TV") device = "Headphones"
else if (device = "Headphones") device = "Speakers"
run, nircmd.exe setdefaultsounddevice device
return

Just cant make this as valid AHK script. Any help is appreciated P.S. nircmd utility allows to set audio device from command line

Thanks.

UPD Updated to this, but %device% is empty for unknown reason. Followed the manual (https://autohotkey.com/docs/Variables.htm), should work, but it doesnt :(

enter image description here

UPD2 As @Blauhim suggested, I had another key binding before it with return statement. Once I moved the script to the top, it worked like a charm. Thanks.

Upvotes: 1

Views: 1765

Answers (1)

phil294
phil294

Reputation: 10872

device := "Speakers" ; alternative: device = Speakers
F2::
if (device = "Speakers") 
    device := "TV" ; had to be on the next line
else if (device = "TV") 
    device := "Headphones" ; had to be on the next line
else if (device = "Headphones") 
    device := "Speakers" ; had to be on the next line
run, nircmd.exe setdefaultsounddevice %device%
return

Upvotes: 3

Related Questions