2

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!

  • Why specifically when the screen turns on - versus when the screensaver goes away? What is your goal? – Dreamspace President Apr 29 '18 at 08:15
  • @DreamspacePresident I wanted to show a current weather dialog whenever the computer resumed / turned on again. And I don't have control over the screensaver. –  Apr 29 '18 at 17:52
  • "I don't have control over the screensaver." Unfortunate. Else these might have helped you: [Detect Windows idle state (JNA)](http://www.rgagnon.com/javadetails/java-detect-windows-idle-state-jna.html), [How to determine if a screensaver is running in Java?](https://stackoverflow.com/questions/2057492/how-to-determine-if-a-screensaver-is-running-in-java) – Dreamspace President Apr 29 '18 at 17:59

3 Answers3

1

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!

user2023577
  • 1,752
  • 1
  • 12
  • 23
0

java.awt.window has a function that reads the following:

getGraphicsConfiguration()

public GraphicsConfiguration getGraphicsConfiguration()

Gets the GraphicsConfiguration associated with this Component. If the Component has not been assigned a specific GraphicsConfiguration, the GraphicsConfiguration of the Component object's top-level container is returned. If the Component has been created, but not yet added to a Container, this method returns null.

Returns:

the GraphicsConfiguration used by this Component 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()

SkiiNet
  • 67
  • 1
  • 8
  • Thanks, but I didn't see anything about monitor state. Did I miss it? –  Apr 27 '18 at 02:40
0

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).

Dreamspace President
  • 1,060
  • 13
  • 33
  • You don't answer the question: you need a screensaver but what the op wants is when the monitor turns off (after the screen saver generally). – C.Champagne Apr 27 '18 at 08:49