Toolmaker
Toolmaker

Reputation: 564

Detecting HDMI cable event within .NET?

I'm trying to detect the event when a HDMI cable is plugged into the HDMI port of my laptop from within the .NET framework. I know that there is an event being triggered, because when the cable is plugged in, Windows makes the obvious "new hardware" sound and changes screen resolution to something more suitable.

I'm getting a bit tired of having the change my audio device to HDMI output by hand so want to write a small app to do it for me.

Upvotes: 4

Views: 4938

Answers (2)

Roni Tovi
Roni Tovi

Reputation: 886

I'm very surprised that noone here mentioned about the DisplaySettingsChanging or DisplaySettingsChanged events. When you plug-in or out an HDMI cable, o/s detects it for you and renumerates the screens. You can catch that events.

An example piece of code would be:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)        
        {

            Microsoft.Win32.SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;

            Microsoft.Win32.SystemEvents.DisplaySettingsChanging += SystemEvents_DisplaySettingsChanging;

            Console.Read();
        }

        static void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
        {
            Console.WriteLine("Display settings have been changed.");
        }

        static void SystemEvents_DisplaySettingsChanging(object sender, EventArgs e)
        {
            Console.WriteLine("Display settings are changing now...");
        }

    }
}

Upvotes: 2

Tergiver
Tergiver

Reputation: 14507

Have you tried WM_DEVICECHANGE? I don't have a way to test it myself.

If you're using Winforms you can override the Control.WndProc method to deal with Windows messages that the framework doesn't wrap.

Upvotes: 2

Related Questions