In java one can get the current graphics environment using
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
A quick and dirty solution would be to batch the execution of an small java program, that just stores the last known graphics environement in a file and execute the notification whenever it changes (execution should be instant, mostly the cost of running the jvm).
It's not elegant, but doing something the native way would need to be cross-platform and would end up ugly too. Unless you don't care about portability.
Choose your poison.