Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adjusting Master Volume Level (sndvol32)...

1,203 views
Skip to first unread message

Blue Streak

unread,
Mar 4, 2008, 1:42:17 PM3/4/08
to
Hello,

I am trying to develop a script that will adjust the master volume
control on a computer. The best example that I have been able to find
is the following:

Set oShell=CreateObject("Wscript.Shell")
iWait = 1000
oShell.Run "sndvol32"
WScript.Sleep iWait
oShell.AppActivate "Volume Control"
WScript.Sleep iWait
oShell.SendKeys("{TAB}{END}")
WScript.Sleep (iWait/2)
oShell.SendKeys("{PGUP}")
WScript.Sleep (iWait/2)
oShell.AppActivate "Volume Control"
oShell.SendKeys "%{F4}"

Is there something that like an ActiveX object or WMI control that I
can use instead where I can set the volume=10 or 10%?

TIA...

Tom Lavedas

unread,
Mar 4, 2008, 3:17:53 PM3/4/08
to

It doesn't appear that the WMI Win32_SoundDevice class has any method
to control the volume. According to MS documentation at MSDN, it only
has two methods, Reset and SetPowerSomething. Neither of which
provides any help.

In addition, SndVol32.exe only has two command line switches, -R to
reset it into record mode and -D to ID a device and no arguments. So
that's a bust, too.

The only thing I can offer is an improved version of your script ...

with CreateObject("Wscript.Shell")
.Run "sndvol32"
do until .AppActivate("Volume") : wsh.Sleep 50 : Loop
.SendKeys "{TAB}{END}" : wsh.Sleep 50
.SendKeys "{PGUP}" : wsh.Sleep 50
.SendKeys "%{F4}" : wsh.Sleep 50
end with

It will take a lot less than three seconds to function. I noted that
on my XP machine sndvol32 opens a control panel with the title of
"Master Volume", so I shortened the title to work with both your
version and my version of the title. I also closed the loop around
the AppActivate to make the process a little more robust.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

Jeff Dillon

unread,
Mar 6, 2008, 8:30:05 PM3/6/08
to
Ouch! Never use SendKeys. It will send keys to the currently active window,
which can change at any time, of course

Jeff

"Tom Lavedas" <tglb...@cox.net> wrote in message
news:742b2426-49ed-4b3c...@m34g2000hsc.googlegroups.com...

Tom Lavedas

unread,
Mar 7, 2008, 8:20:56 AM3/7/08
to
On Mar 6, 8:30 pm, "Jeff Dillon" <jeffdil...@hotmailremove.com> wrote:
> Ouch! Never use SendKeys. It will send keys to the currently active window,
> which can change at any time, of course
>
> Jeff
>
> "Tom Lavedas" <tglba...@cox.net> wrote in message

>
> news:742b2426-49ed-4b3c...@m34g2000hsc.googlegroups.com...
>
> > On Mar 4, 1:42 pm, Blue Streak <rdlebre...@hotmail.com> wrote:
> >> Hello,
>
> >> I am trying to develop a script that will adjust the master volume
> >> control on a computer. The best example that I have been able to find
> >> is the following:
>
{snip}

Though I understand your intent, "never" is a fighting word -
especially when you offer no alternative.

Yes, the keystrokes can go astray, but sometimes its still the most
efficient way to do a little task for your own purposes. It probably
should be avoided in any 'production' environment, but I still think
there are a few simple tasks that can benefit from judicial use of
Sendkeys.

Clearly, the 'open-loop' examples generally given in the documentation
are an abomination, but with liberal use of AppActivate to keep the
focus on the right window, it's sometimes just quicker to write a
keystroke driven process than searching for mr. unreliable's
tutorials, as good and helpful as they are, on accessing the system
kernel's APIs - and then finding some reference to the right one - and
then coding it.

Nuf said.

(Note: This signature was inserted using a Sendkeys routine ;o)

Paul Randall

unread,
Mar 7, 2008, 10:29:52 AM3/7/08
to

"Blue Streak" <rdleb...@hotmail.com> wrote in message
news:7ee53453-7bc0-4ce2...@c33g2000hsd.googlegroups.com...

On my system, your sample script sets the volume to 20%. Page up and
down result in 20% changes and the up and down arrows result in 0.2%
changes. So it would take 50 up arrows to get to 10%. I can send 50
up arrow keystrokes with:
oShell.SendKeys("{UP 50}")
but this takes about 5 seconds to complete; any window that pops up
within that time frame could steal those keystrokes.

Perhaps AutoItX control would be more reliable to send those
keystrokes to the window of interest.
http://www.autoitscript.com/autoit3/

-Paul Randall


Jeff Dillon

unread,
Mar 7, 2008, 1:26:49 PM3/7/08
to
Nope, never do that. Bad practice.

"Tom Lavedas" <tglb...@cox.net> wrote in message

news:c3905aeb-b2fc-4d4c...@m3g2000hsc.googlegroups.com...

Jeff Dillon

unread,
Mar 7, 2008, 1:35:20 PM3/7/08
to
What a hack. How far did you look? 5 seconds searching found many, including

http://www.brothersoft.com/sound-volume-activex-control-24152.html

"Tom Lavedas" <tglb...@cox.net> wrote in message

news:742b2426-49ed-4b3c...@m34g2000hsc.googlegroups.com...

asdfasdf

unread,
Feb 16, 2010, 3:48:06 AM2/16/10
to
I know that this thread is ancient but I didnt see the answer that I would have wanted here, so heres my best shot. This will work regardless of which window is active. They are the volume control buttons on your keyboard.

Volume Mute=sendkeys chr(173)
Volume Down=sendkeys chr(174)
Volume Up=sendkeys chr(175)

If you really need to set the volume to a certain position you can lower it to 0 (a known position) and then use sendkeys chr(175) until you have reached the desired volume. However, if you wanted to change the volume and then set it back to its original position you are still screwed, because you cant determine the original volume using this method. Im sure you could detect the volume level somewhere in the registry, but the specific location varies based on the installed sound card. Good Luck

Blue Streak wrote:

Adjusting Master Volume Level (sndvol32)...
06-Mar-08

Hello,

TIA...

Previous Posts In This Thread:

On Thursday, March 06, 2008 2:48 AM
Blue Streak wrote:

Adjusting Master Volume Level (sndvol32)...
Hello,

TIA...

On Thursday, March 06, 2008 2:49 AM
Tom Lavedas wrote:

Re: Adjusting Master Volume Level (sndvol32)...


On Mar 4, 1:42 pm, Blue Streak <rdlebre...@hotmail.com> wrote:

It doesn't appear that the WMI Win32_SoundDevice class has any method
to control the volume. According to MS documentation at MSDN, it only
has two methods, Reset and SetPowerSomething. Neither of which
provides any help.

In addition, SndVol32.exe only has two command line switches, -R to
reset it into record mode and -D to ID a device and no arguments. So
that's a bust, too.

The only thing I can offer is an improved version of your script ...

with CreateObject("Wscript.Shell")
.Run "sndvol32"
do until .AppActivate("Volume") : wsh.Sleep 50 : Loop
.SendKeys "{TAB}{END}" : wsh.Sleep 50
.SendKeys "{PGUP}" : wsh.Sleep 50
.SendKeys "%{F4}" : wsh.Sleep 50
end with

It will take a lot less than three seconds to function. I noted that
on my XP machine sndvol32 opens a control panel with the title of
"Master Volume", so I shortened the title to work with both your
version and my version of the title. I also closed the loop around
the AppActivate to make the process a little more robust.

On Thursday, March 06, 2008 8:30 PM
Jeff Dillon wrote:

Ouch!


Ouch! Never use SendKeys. It will send keys to the currently active window,
which can change at any time, of course

Jeff

On Friday, March 07, 2008 10:29 AM
Paul Randall wrote:

Re: Adjusting Master Volume Level (sndvol32)...


"Blue Streak" <rdleb...@hotmail.com> wrote in message
news:7ee53453-7bc0-4ce2...@c33g2000hsd.googlegroups.com...

On my system, your sample script sets the volume to 20%. Page up and

down result in 20% changes and the up and down arrows result in 0.2%
changes. So it would take 50 up arrows to get to 10%. I can send 50
up arrow keystrokes with:
oShell.SendKeys("{UP 50}")
but this takes about 5 seconds to complete; any window that pops up
within that time frame could steal those keystrokes.

Perhaps AutoItX control would be more reliable to send those
keystrokes to the window of interest.
http://www.autoitscript.com/autoit3/

-Paul Randall

On Friday, March 07, 2008 1:26 PM
Jeff Dillon wrote:

Re: Adjusting Master Volume Level (sndvol32)...


Nope, never do that. Bad practice.

On Friday, March 07, 2008 1:35 PM
Jeff Dillon wrote:

What a hack. How far did you look?
What a hack. How far did you look? 5 seconds searching found many, including

http://www.brothersoft.com/sound-volume-activex-control-24152.html

On Saturday, March 08, 2008 8:45 AM
Tom Lavedas wrote:

Re: Adjusting Master Volume Level (sndvol32)...


On Mar 6, 8:30 pm, "Jeff Dillon" <jeffdil...@hotmailremove.com> wrote:

{snip}

Though I understand your intent, "never" is a fighting word -
especially when you offer no alternative.

Yes, the keystrokes can go astray, but sometimes its still the most
efficient way to do a little task for your own purposes. It probably
should be avoided in any 'production' environment, but I still think
there are a few simple tasks that can benefit from judicial use of
Sendkeys.

Clearly, the 'open-loop' examples generally given in the documentation
are an abomination, but with liberal use of AppActivate to keep the
focus on the right window, it's sometimes just quicker to write a
keystroke driven process than searching for mr. unreliable's
tutorials, as good and helpful as they are, on accessing the system
kernel's APIs - and then finding some reference to the right one - and
then coding it.

Nuf said.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/


(Note: This signature was inserted using a Sendkeys routine ;o)


Submitted via EggHeadCafe - Software Developer Portal of Choice
Putting Twitter Realtime Search to Work
http://www.eggheadcafe.com/tutorials/aspnet/6100d85b-2f27-472d-af24-c9960b55b669/putting-twitter-realtime.aspx

asdfasdf

unread,
Feb 16, 2010, 3:49:01 AM2/16/10
to
I know that this thread is ancient but I didnt see the answer that I would have wanted here, so heres my best shot. This will work regardless of which window is active. They are the volume control buttons on your keyboard.

Volume Mute=sendkeys chr(173)
Volume Down=sendkeys chr(174)
Volume Up=sendkeys chr(175)

If you really need to set the volume to a certain position you can lower it to 0 (a known position) and then use sendkeys chr(175) until you have reached the desired volume. However, if you wanted to change the volume and then set it back to its original position you are still screwed, because you cant determine the original volume using this method. Im sure you could detect the volume level somewhere in the registry, but the specific location varies based on the installed sound card. Good Luck

Blue Streak wrote:

Adjusting Master Volume Level (sndvol32)...
06-Mar-08

Hello,

TIA...

Previous Posts In This Thread:

On Thursday, March 06, 2008 2:48 AM
Blue Streak wrote:

Adjusting Master Volume Level (sndvol32)...

Hello,

TIA...

On Thursday, March 06, 2008 2:49 AM
Tom Lavedas wrote:

Jeff

-Paul Randall

http://www.brothersoft.com/sound-volume-activex-control-24152.html

Nuf said.

On Tuesday, February 16, 2010 3:48 AM
asdf asdf wrote:

the BEST sendkeys method


I know that this thread is ancient but I didnt see the answer that I would have wanted here, so heres my best shot. This will work regardless of which window is active. They are the volume control buttons on your keyboard.

Volume Mute=sendkeys chr(173)
Volume Down=sendkeys chr(174)
Volume Up=sendkeys chr(175)

If you really need to set the volume to a certain position you can lower it to 0 (a known position) and then use sendkeys chr(175) until you have reached the desired volume. However, if you wanted to change the volume and then set it back to its original position you are still screwed, because you cant determine the original volume using this method. Im sure you could detect the volume level somewhere in the registry, but the specific location varies based on the installed sound card. Good Luck

Submitted via EggHeadCafe - Software Developer Portal of Choice

EggHeadCafe Chat Chaos in Silverlight Released Today
http://www.eggheadcafe.com/tutorials/aspnet/325ea67e-d6c4-4811-b096-54f31bdede5d/eggheadcafe-chat-chaos-in.aspx

burritobandito

unread,
Feb 16, 2010, 3:50:20 AM2/16/10
to
I know that this thread is ancient but I didnt see the answer that I would have wanted here, so heres my best shot. This will work regardless of which window is active. They are the volume control buttons on your keyboard.

Volume Mute=sendkeys chr(173)
Volume Down=sendkeys chr(174)
Volume Up=sendkeys chr(175)

If you really need to set the volume to a certain position you can lower it to 0 (a known position) and then use sendkeys chr(175) until you have reached the desired volume. However, if you wanted to change the volume and then set it back to its original position you are still screwed, because you cant determine the original volume using this method. Im sure you could detect the volume level somewhere in the registry, but the specific location varies based on the installed sound card. Good Luck

Blue Streak wrote:

Adjusting Master Volume Level (sndvol32)...
06-Mar-08

Hello,

TIA...

Previous Posts In This Thread:

On Thursday, March 06, 2008 2:48 AM
Blue Streak wrote:

Adjusting Master Volume Level (sndvol32)...

Hello,

TIA...

On Thursday, March 06, 2008 2:49 AM
Tom Lavedas wrote:

Jeff

-Paul Randall

http://www.brothersoft.com/sound-volume-activex-control-24152.html

Nuf said.

Get Started with SQLite and Visual Studio
http://www.eggheadcafe.com/tutorials/aspnet/20f7912e-6fa7-40eb-b31b-b6f46d4f2c6a/get-started-with-sqlite-a.aspx

Evertjan.

unread,
Feb 16, 2010, 4:48:01 AM2/16/10
to
wrote on 16 feb 2010 in microsoft.public.scripting.jscript:

> Set oShell=CreateObject("Wscript.Shell")

Wrong NG, this is NOT J[ava]script

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

0 new messages