posted: August 23, 2010

Stupid Java Trick: Howto Disable Your Screensaver

A bit of background: I use one machine primarily for development, and a second one for communication (email, etc...) My reason for this is to simply control interuptions. But, it does have an anoyance. By the time I need to access the other machine, the screensaver is active. The need to login everytime gets to be a pain. Today, I decided to do something about it.

This trick is pretty low-bar. It basically uses the java.awt.Robot class to press and release the "shift" key every minute. I tried on a few machines and it seems to work well on Linux/Mac/Windows OS's.

The Code

private void blockScreenSaver () throws Exception {
  Robot robot = new Robot();
  while (true) {
    Thread.sleep(60 * 1000); // one minute

That's all there is to it. Just create a main method to call it and you're done. If you want to get fancy, you could always make a little UI and add an interupt to turn it on and off. As for me, I just call it with a one-line shell script and kill it when not needed.

Maybe I should native compile with GCJ and sell the executable for $20 ;-)


by: Paul E Davis


About willCode4Beer