Nyxynyx
Nyxynyx

Reputation: 63599

Python Detect Screensaver Start/Stop Event on Windows

How can a Python 2.7 script detect the start/stop event of a Windows Screensaver?

Upvotes: 1

Views: 1694

Answers (1)

Tom
Tom

Reputation: 1133

You can actually do that with Pywin32. Pywin provides Python bindings for the Win32 API and for COM.

Regarding your question, it allows you to "listen" to windows events - such as start/stop event of a Windows ScreenSaver.

You should be able to get the current state of the screen saver by doing something like this:

import win32gui, win32con

def CheckScreenSaverState():
  return win32gui.SystemParametersInfo(win32con.SPI_GETSCREENSAVERRUNNING)

In this example we use the win32gui which allows access to the GUI of a windows application. By calling win32gui.SystemParametersInfo (which is a function that actually belongs to the windows GUI messaging system - out of scope for this explanation, read more here), we are able to get the state of the screen saver, using the SPI_GETSCREENSAVERRUNNING constant (which is an internal constant of the Windows OS). This method I wrote should return a boolean value of the state of the screen saver (True is running False if not).

I didn't have time to test it, but tell me how it went I might be able to help you further.

Good luck, Tom.

Upvotes: 2

Related Questions