thmsdrew has asked for the wisdom of the Perl Monks concerning the following question:
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.cplJust 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");
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Programmatically enable/disable a playback device in Windows 7
by Anonymous Monk on Nov 08, 2012 at 17:09 UTC | |
Re: Programmatically enable/disable a playback device in Windows 7
by bulk88 (Priest) on Nov 08, 2012 at 16:31 UTC | |
by thmsdrew (Scribe) on Nov 08, 2012 at 17:26 UTC | |
Re: Programmatically enable/disable a playback device in Windows 7
by bulk88 (Priest) on Nov 09, 2012 at 04:32 UTC | |
Re: Programmatically enable/disable a playback device in Windows 7
by thmsdrew (Scribe) on Nov 09, 2012 at 02:27 UTC |