Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Toggle set default audio device in Windows 7.


  • Please log in to reply
18 replies to this topic
Void
  • Members
  • 9 posts
  • Last active: Jul 18 2011 01:47 PM
  • Joined: 16 Jun 2011
I made this Script to quickly swap default audio device in Windows 7.
It will toggle set default audio device from your playback device list between two with a press of one button "ScrollLock" in this case. I use it to swap the sound from my computer speaker to my G35 Headset.

ScrollLock::
 {
   toggle:=!toggle ;toggles up and down states.

   if toggle
    {
    Run, mmsys.cpl
    WinWait,[color=red]Sound[/color] ; Change "Sound" to the name of the window in your local language
    ControlSend,SysListView321,{Down [color=red]1[/color]} ; This number selects the matching audio device in the list, change it accordingly
    ControlClick,[color=red]&Set Default[/color] ; Change "&Set Default" to the name of the button in your local language
    ControlClick,OK
    }
   if !toggle
    {
    Run, mmsys.cpl
    WinWait,[color=red]Sound[/color] ; Change "Sound" to the name of the window in your local language
    ControlSend,SysListView321,{Down [color=red]2[/color]} ; This number selects the matching audio device in the list, change it accordingly
    ControlClick,[color=red]&Set Default[/color] ; Change "&Set Default" to the name of the button in your local language
    ControlClick,OK
    }
 }
return


aaffe
  • Members
  • 1045 posts
  • Last active: Jan 16 2014 01:32 PM
  • Joined: 17 May 2007
a bit shorter:
ScrollLock:: 
  toggle:=!toggle ;toggles up and down states. 
  Run, mmsys.cpl 
  WinWait,Sound ; Change "Sound" to the name of the window in your local language 
  if toggle
    ControlSend,SysListView321,{Down 1} ; This number selects the matching audio device in the list, change it accordingly 
  Else
    ControlSend,SysListView321,{Down 2} ; This number selects the matching audio device in the list, change it accordingly 
  ControlClick,&Set Default ; Change "&Set Default" to the name of the button in your local language 
  ControlClick,OK 
return


Void
  • Members
  • 9 posts
  • Last active: Jul 18 2011 01:47 PM
  • Joined: 16 Jun 2011
Thanks for refining it :) its the first script i made, taking some parts from other examples till i got it working.

rodfell
  • Members
  • 138 posts
  • Last active: Jun 26 2011 10:02 PM
  • Joined: 05 Oct 2007
excellent. thanks for that. i need to make this change a lot now. With windows xp you could use 2 sound devices at once. now you need to specify one. Found I needed a few sleeps and added the curious na parameter to work reliably on my system.
ScrollLock:: 

  toggle:=!toggle ;toggles up and down states. 

  Run, mmsys.cpl 

  WinWait,Sound ; Change "Sound" to the name of the window in your local language 

  if toggle

    ControlSend,SysListView321,{Down 1} ; This number selects the matching audio device in the list, change it accordingly 

  Else

    ControlSend,SysListView321,{Down 2} ; This number selects the matching audio device in the list, change it accordingly 

  sleep 100

  ControlClick,&Set Default,Sound,,,,na ; Change "&Set Default" to the name of the button in your local language 

  sleep 100

  ControlClick,OK,Sound,,,,na

return


Void
  • Members
  • 9 posts
  • Last active: Jul 18 2011 01:47 PM
  • Joined: 16 Jun 2011

excellent. thanks for that. i need to make this change a lot now. With windows xp you could use 2 sound devices at once. now you need to specify one. Found I needed a few sleeps and added the curious na parameter to work reliably on my system.


What exactly does Sound,,,,na do? i sometimes exerience that this script fails. dose na solve this together with sleep 100? i set sleep to 10 instead found 100 to be a bit slow.

rodfell
  • Members
  • 138 posts
  • Last active: Jun 26 2011 10:02 PM
  • Joined: 05 Oct 2007

What exactly does Sound,,,,na do?

It says in the autohotkey help file that it can add to the reliability of controlclick. To quote - "NA" avoids marking the target window as active and avoids merging its input processing with that of the script, which may prevent physical movement of the mouse from interfering (but usually only when the target window is not active). However, this method might not work for all types of windows and controls.
The sleep prevents the next controlclick being sent before the window can process it. You can read about this in the setkeydelay entry of the help file
i like the sleep 100 myself as it gives you a glimpse to see that the actions have actually worked

ekw
  • Members
  • 6 posts
  • Last active: Jul 04 2011 01:11 PM
  • Joined: 17 May 2011
would there be a way to add into this a toggle for the monitor ?

I have a HTPC set up.
when I am using it as a "regular computer", I want sound from the speakers and video to the desktop monitor.
when I am using it to watch live or recorded TV, I want the audio to go to the hdmi output and the video to go to the television (which the pc sees as a second monitor hooked to the hdmi port.

so, ideally, I would be able to have a single ahk script that swapped the monitor & sound at the same time.

I searched for monitor toggles, but all I found was old registry based toggles for XP and Vista.
I am running Win 7, 64bit premium, if it matters.

thanks.

Stef
  • Guests
  • Last active:
  • Joined: --
What it does in my case is merely to open my sound device's menu (from which I manually change the device) instead of automatically changing to the according device. And after that it hangs (I have to reload the script so that it would work again...)

Also I have changed the lines of codes requiring a dif language (as per the comments' instructions) to my windows' language (Greek in my case). But still nothing. The behavior of the script remains the same...

Any advice? Thanks

Stef
  • Guests
  • Last active:
  • Joined: --

What it does in my case is merely to open my sound device's menu (from which I manually change the device) instead of automatically changing to the according device. And after that it hangs (I have to reload the script so that it would work again...)

Also I have changed the lines of codes requiring a dif language (as per the comments' instructions) to my windows' language (Greek in my case). But still nothing. The behavior of the script remains the same...

Any advice? Thanks


Hmm, apparently Hotkey does not get Greek characters (it hangs in line 4, where I used the greek characters)...too bad.

Is there any way to make Auto-hotkey to support UTF-8 character encoding?

Mate
  • Guests
  • Last active:
  • Joined: --
I think I found the solution for our problem:
Media_Stop::
	toggle:=!toggle
	Run, mmsys.cpl 
	WinWait,Sound ; Change "Sound" to the name of the window in your local language 
	if toggle
		ControlSend,SysListView321,{Down 1} ; This number selects the matching audio device in the list, change it accordingly 
	Else
		ControlSend,SysListView321,{Down 2} ; This number selects the matching audio device in the list, change it accordingly 
	Click 202, 390,
	Sleep, 10
	ControlClick,OK
return


clpratt
  • Members
  • 1 posts
  • Last active: Aug 26 2012 07:51 PM
  • Joined: 26 Aug 2012
I have tried the 3 scripts below to swap default sound device between headphones and USB speakers. All work, sort of, but require sometimes up to 20 key pushes before it works. I've tried putting sleep delays in various places to no effect. Has anyone got any suggestions please.
I'm running W7 64bit.

Script #1.........

ScrollLock::
toggle:=!toggle ;toggles up and down states.
Run, mmsys.cpl
WinWait,Sound ; Change "Sound" to the name of the window in your local language
if toggle
ControlSend,SysListView321,{Down 4} ; This number selects the matching audio device in the list, change it accordingly
Else
ControlSend,SysListView321,{Down 7} ; This number selects the matching audio device in the list, change it accordingly
ControlClick,&Set Default ; Change "&Set Default" to the name of the button in your local language
ControlClick,OK
return

Script#2................

;
; AutoHotkey Version: 1.x
; Language: English
; Platform: Win9x/NT
; Author: A.N.Other <[email protected]>
;
; Script Function:
; Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
;

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

*F7::
Run, mmsys.cpl
Winwait,Sound
ControlSend,SysListView321,{Down 7}
ControlClick,&Set Default
ControlClick,OK
return

*F4::
Run, mmsys.cpl
Winwait,Sound
ControlSend,SysListView321,{Down 4}
ControlClick,&Set Default
ControlClick,OK
return

Script#3................

;
; AutoHotkey Version: 1.x
; Language: English
; Platform: Win9x/NT
; Author: A.N.Other <[email protected]>
;
; Script Function:
; Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
;
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

ScrollLock::
toggle:=!toggle
Run, mmsys.cpl
WinWait,Sound ; Change "Sound" to the name of the window in your local language
if toggle
ControlSend,SysListView321,{Down 4} ; This number selects the matching audio device in the list, change it accordingly
Else
ControlSend,SysListView321,{Down 7} ; This number selects the matching audio device in the list, change it accordingly
ControlClick,&Set Default ; Change "&Set Default" to the name of the button in your local language
Click 202, 390,
Sleep, 10
ControlClick,OK
return

regards Chris

FatZgrED
  • Members
  • 14 posts
  • Last active: Nov 24 2012 04:14 PM
  • Joined: 19 Oct 2008
Maybe mine will work for you.
I wrote mine because those didnt work for me either.
It looks for a sound card that is default and every time u press hotkey toggles cards in range

^#s::
def_var:="default"		;default in your language
setDef_var:="Set default" ;Set default in your language
minDv=1 ;start switching here
maxDv=2 ;stop here
	Run, RunDll32.exe shell32.dll`,Control_RunDLL mmsys.cpl`,`,0,,,dllpid
	WinWaitActive ,ahk_pid %dllpid%,,5
	ControlGet, slvHwnd,Hwnd, , SysListView321, ahk_pid %dllpid%
	ControlGet, dvCnt,List, Count, ,ahk_id %slvHwnd%
	ControlGet, dvLst,List, , ,ahk_id %slvHwnd%
	Loop, Parse, dvLst, `n
	if InStr(A_LoopField,def_var)
			dDv=%A_Index% ;%A_LoopField%.

	If (dDv < minDv)
		sel:=minDv
	Else	
		If (dDv >= maxDv)	
			sel:=minDv
		Else
			sel:=dDv+1
	ControlSend , , {Down %sel%}, ahk_id %slvHwnd%
	Sleep 30
	ControlClick,&%setDef_var%
	ControlClick,OK
Return


mhe
  • Members
  • 40 posts
  • Last active: Dec 04 2015 10:29 PM
  • Joined: 18 Oct 2007
nircmd http://www.nirsoft.n...ils/nircmd.html can do this.

nircmd.exe showsounddevices
nircmd.exe setdefaultsounddevice "insertDevicenameHere"

Tazuna
  • Members
  • 1 posts
  • Last active: Oct 11 2013 01:13 AM
  • Joined: 10 Oct 2013

This seems to work for me. The window goes away.

ScrollLock::

  toggle:=!toggle

  Run, mmsys.cpl

  Winhide,Sound

  WinWait,Sound

  if toggle

    ControlSend,SysListView321,{Down 1}

  Else

    ControlSend,SysListView321,{Down 2}

  sleep 50

  ControlClick,&Set Default,Sound,,,,na

  sleep 50

  ControlClick,OK,Sound,,,,na

  sleep 50

  WinClose,Sound

Return


JaseUK
  • Members
  • 1 posts
  • Last active: Oct 14 2013 08:30 AM
  • Joined: 13 Oct 2013

 

This seems to work for me. The window goes away.

ScrollLock::

  toggle:=!toggle

  Run, mmsys.cpl

  Winhide,Sound

  WinWait,Sound

  if toggle

    ControlSend,SysListView321,{Down 1}

  Else

    ControlSend,SysListView321,{Down 2}

  sleep 50

  ControlClick,&Set Default,Sound,,,,na

  sleep 50

  ControlClick,OK,Sound,,,,na

  sleep 50

  WinClose,Sound

Return

 

I have to say Tazuna this is the only script that seems to work flawlessly with Windows 8. All the other scripts I used only toggled the sound output once and that's it.