Supersonic Man

February 4, 2012

instant volume control

Filed under: Hobbyism and Nerdry — Supersonic Man @ 9:38 pm

Let’s say you’re at work and listening to MP3s in your headphones.  And someone comes up and needs to talk.  You have to stop the music… and in the past I’ve found I’m always fumbling to open up the player GUI and hit Pause, and it takes several seconds, during which you’re not looking very professional.

And if you use the taskbar icon to adjust the volume, it makes a loud blatt in your ear as soon as you let go of it.  That’s not much good either.

I decided I wanted a quick keyboard-shortcut way to go play/pause, and to increase or decrease the volume.  Only problem was, at work I’m not allowed to install any outside software.  So it had to be done with nothing but a script.  Turns out the Windows Script Host can emulate the special media-player keys on a multimedia keyboard, like so:

/*
To use, create a desktop shortcut with command line argument V+n, V-n, or P,
and give that shortcut a hotkey.  That hotkey will then be a quick way to
operate the media player, emulating a keyboard with built-in multimedia keys.
*/

// CONSTANTS:  (I wish JScript actually had such a thing...)
var VOLUME_MUTE  = "\xAD";
var VOLUME_DOWN  = "\xAE";
var VOLUME_UP    = "\xAF";
var NEXT_TRACK   = "\xB0";
var PREV_TRACK   = "\xB1";
var STOP         = "\xB2";
var PAUSE_RESUME = "\xB3";


var wshell = new ActiveXObject("WScript.Shell");
if (WScript.Arguments.Length == 0)
{
    WScript.Echo("Usage:  V+n to increase volume, V-n to decrease, P to Play/Pause");
}
else
{
    var arg = WScript.Arguments(0).toUpperCase();
    if (arg.charAt(0) == 'V')
    {
        var bump = parseInt(arg.substring(1));
        if (bump > 0)
            for (var i = 0; i < Math.min(bump, 30); i++)
                wshell.SendKeys(VOLUME_UP);        // bumps it about 4%
        else if (bump < 0)
            for (var i = 0; i > Math.max(bump, -30); i--)
                wshell.SendKeys(VOLUME_DOWN);    // about 4%
        else
            WScript.Echo("V must be followed by a positive or negative number");
    }
    else if (arg.charAt(0) == 'P')
        wshell.SendKeys(PAUSE_RESUME);
    else
        WScript.Echo("Command '" + arg + "' not recognized");
}

So this script can, with command-line arguments, do the things we need.  Now all you need to to to make hotkeys do the job is to create a shortcut to the script on your desktop.  I created three: “Volume Up”, “Volume Down”, and “Pause ~ Resume”.  The first has a command-line argument of V+2 and a shortcut key of Ctrl+Alt+Numeric-pad Plus.  The second has an argument of V-3 and shortcut key of Ctrl+Alt+Numeric-pad Minus.  The third has an argument of P on the command line and a shortcut of Ctrl+Alt+Numeric-pad Asterisk.  (I tried to use the Windows key but the desktop keyboard-shortcut thing won’t support it.)

The reason I had the volume go down by three but up by two is firstly, because I usually need to decrease the volume more urgently than I need to increase it, and secondly, having them differ by one allows me to fine-tune a desired level by going back and forth a bit.

[Update] Eek — under Windows XP, when I tried hitting the key a few times while the computer was a bit busy, the desktop locked up until I used ctrl-alt-del.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: