Hotkey to toggle between speakers and headphones

If you have macros or EventGhost Configuration Tree items you wish to share this is the place to do it.
Post Reply
User avatar
Plugin Developer
Posts: 84
Joined: Sat Aug 12, 2017 7:52 pm
Location: Ottawa, Canada

Hotkey to toggle between speakers and headphones

Post by Boolean263 » Sat Aug 12, 2017 8:56 pm

Hi all! I'm a new user to this forum, but I've been playing on and off with EventGhost for a while, and like to play with Python for fun. I've created my first non-trivial EG event/action and I wanted to share.

This code uses the AudioEndpoint plugin in a Python script. When the event triggers (currently by hotkey), the script finds the audio devices that correspond to my speakers and my Logitech headset, figures out which one is active, and switches to the other.

Why do it this way? My headset is USB-connected, and the ID that represents it in EventGhost was changing from time to time, breaking my action. Now the ID can be whatever it wants, as long as I name the audio device properly. In my Windows settings, I've named my speakers "Speakers" and my headset "Logitech headset", and this event just checks for names that start with one of those two.

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="0.5.0-rc4">
    <Macro Name="Audio device" XML_Guid="{BF169A5E-6DE2-481E-96A7-08929DF82E84}" Expanded="True">
        <Event Name="Keyboard.Ctrl+Alt+M" XML_Guid="{C5FCABBA-4125-469C-A9D0-0806FBB0C93A}" />
        <Action Name="Python Script: smart switch audio device" XML_Guid="{53DD2A2D-00C9-43F4-9EB6-B54C30889183}">
            EventGhost.PythonScript(u'audioConsole = 0\naudioMultimedia = 1\n\naep = eg.plugins.AudioEndpoint.plugin\nddef = None\ndspk = None\ndear = None\nfor d in aep.AudioDevices:\n    if d.isDefault():\n        ddef = d\n    if d.getName()[0:8] == "Speakers":\n        dspk = d\n    elif d.getName()[0:8] == "Logitech":\n        dear = d\n\n\nif ddef == dspk:\n    aep.AudioDevices.SetDefault(dear, audioConsole)\nelse:\n    aep.AudioDevices.SetDefault(dspk, audioConsole)\n')
I'd appreciate any feedback you have for me. I hope someone else finds this useful or inspiring!
Windows 10 Home, version 1703, build 15063.540 · EventGhost 0.5.0-rc4 · wxPython
Python programmer for fun and profit. Dabbler in EventGhost plugin writing. Wiki gardener. General geek.

Post Reply