Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hello folks. My goal is to programmatically enable or disable playback devices in Windows 7. I have headphones and speakers that I switch between frequently enough that it would be nice to automate it. Right now, if I want to switch between the two, I have to right-click the sound icon in my taskbar, click "Playback devices", right-click the currently enabled device, disabling it, and then right-click the disabled one, enabling it.

I've already looked into Microsoft's DevCon tool. It almost does what I describe, except it can only enable/disable the entire HDAUDIO device. I want to enable/disable either "Headphones" or "Speakers", which I don't think are given individual device IDs.

I did a bit of CPAN searching and didn't find much. I'm thinking maybe there's an automation "suite" that can access windows and click stuff within them. (XPath or something).

Any ideas?

Edit: I can programmatically open up the sound options window I'm talking about with the following command:

C:\> control mmsys.cpl

Just need to be able to identify entities within the window and left- or right-click on them. I think Win32::GuiTest can do that.

Double Edit: This is how I am doing it, for anyone that still cares:

Enable headphones:

#!/usr/bin/perl use strict; use warnings; use Win32::GuiTest qw(FindWindowLike GetWindowRect MouseMoveAbsPix Sen +dMouse SendKeys SetForegroundWindow PushButton); system "control mmsys.cpl"; sleep 1; my @sound = FindWindowLike(undef, "Sound"); my @list = FindWindowLike($sound[0], "", "SysListView32"); my ($left, $top, $right, $bottom) = GetWindowRect($list[0]); MouseMoveAbsPix($right - 7, $top + 57); SendMouse("{RightClick}"); SendKeys("{E}"); MouseMoveAbsPix($right - 7, $top + 112); SendMouse("{RightClick}"); SendKeys("{D}"); SetForegroundWindow($sound[0]); PushButton("OK");

Enable speakers:

#!/usr/bin/perl use strict; use warnings; use Win32::GuiTest qw(FindWindowLike GetWindowRect MouseMoveAbsPix Sen +dMouse SendKeys SetForegroundWindow PushButton); system "control mmsys.cpl"; sleep 1; my @sound = FindWindowLike(undef, "Sound"); my @list = FindWindowLike($sound[0], "", "SysListView32"); my ($left, $top, $right, $bottom) = GetWindowRect($list[0]); MouseMoveAbsPix($right - 7, $top + 57); SendMouse("{RightClick}"); SendKeys("{D}"); MouseMoveAbsPix($right - 7, $top + 112); SendMouse("{RightClick}"); SendKeys("{E}"); SetForegroundWindow($sound[0]); PushButton("OK");

In reply to Programmatically enable/disable a playback device in Windows 7 by thmsdrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (13)
    As of 2015-07-28 23:08 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (260 votes), past polls