Is there a way in Java to detect when the monitor backlight turns on?
I'm using Windows 8.1.
I just want to run a function whenever the screen turns on, like when you wiggle the mouse after 20 minutes.
Thanks!
Is there a way in Java to detect when the monitor backlight turns on?
I'm using Windows 8.1.
I just want to run a function whenever the screen turns on, like when you wiggle the mouse after 20 minutes.
Thanks!
Not that I know of.
I used JNA library (which requires a .dll/.so in your path) to call native OS apis to poll if screensaver was on or not (win32) or pull the lib pthread self thread id on linux, for examples.
But you will need to dig a bit to learn the native api related (not that hard to google). JNA will be the learning curve, but once you got it, it's quite enabling!
There are also callbacks in JNA, but I didn't get to try them. Beware, this is definitely not pure java!
java.awt.window
has a function that reads the following:
getGraphicsConfiguration()
public GraphicsConfiguration getGraphicsConfiguration()
Gets the
GraphicsConfiguration
associated with thisComponent
. If theComponent
has not been assigned a specificGraphicsConfiguration
, theGraphicsConfiguration
of theComponent
object's top-level container is returned. If theComponent
has been created, but not yet added to aContainer
, this method returns null.Returns:
the
GraphicsConfiguration
used by thisComponent
or null
This might not be exactly what you are looking for but you may work around this and maybe use it to detect if a monitor is null or not.
Full docs: https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#getGraphicsConfiguration()
I'm pretty sure there is no pure Java way. But if your monitor screensaver situation is entirely in your hands, you can use a pure black screensaver.
This solution is not rock solid, but it may hit the spot >95% of the time.
1) Set a pure black screensaver in accordance with your energy settings (i.e. when the screen should turn off).
2) Have a 0.5 second interval Thread check if the mouse's position on screen was changed.
3) If so, use Robot's createScreenCapture() to get a BufferedImage of the whole screen.
4) Analyze the image. Fastest way is to directly access its bytes: final DataBufferByte dbi = (DataBufferByte) img.getRaster().getDataBuffer();
If it's pure black, the backlight should still be off.
To save cycles (The screenshot + analysis would eat a lot of energy.), you could prevent the screen capture stuff until the mouse hasn't moved for 20 minutes or whatever your energy time was. Maybe there's a Java-way to obtain this information from the system so you don't have to change your parameters in two places (Windows and your app).