Monitoring a displays state in python?

How can I tell when Windows is changing a monitors power state?

Asked by: Kelvin884 | Posted: 27-01-2022

Answer 1

It seems that, when Windows wants to start the screen saver or turn the monitor off, it will send a WM_SYSCOMMAND to the topmost window with a wParam of SC_SCREENSAVE (to start the screen saver) or a wParam of SC_MONITORPOWER and a lParam of 1 or 2 (to turn the monitor off). This message will then be passed to DefWindowProc, which will actually do the action. So, if your window happens to be the topmost one, you can intercept these events and ignore them (or do anything else you want before passing them to DefWindowProc).

On Windows Vista, there seems to be a more intuitive, and more reliable, way to know the monitor power state. You call RegisterPowerSettingNotification to tell the system to send your window a WM_POWERBROADCAST message with a wParam of PBT_POWERSETTINGCHANGE and a lParam pointing to a POWERBROADCAST_SETTING structure.

I cannot test either of them since I currently do not have any computer with Windows nearby. I hope, however, they point you in the right direction.


Answered by: Roland273 | Posted: 28-02-2022

Similar questions

monitoring - CPU Usage Per Process in Python

Is it possible for me to see the amount of processor usage (% of maximum) that the current, python, app is using? Scenario: My host will allow me to run my app as long as it does not consume more then X% of the CPU power, so I would like it to 'keep an eye on itself' and slowdown. So how can I know how much CPU the app is using? Target platform is *nix, however I would like to do it on a Win host also.

django - Does anyone know of a python based web ui for snmp monitoring?

Closed. This question does not meet Stack Overflow guid...

Should I use Perl or Python for network monitoring?

python - Monitoring user idle time

Developing a mac app, how can I tell whether the user is currently at their computer or not? Or how long ago they last pressed a key or moved the mouse?

python - Monitoring internet activity

I'm looking into writing a small app (in Python) that monitors internet activity. The same idea as NetMeter except with a little more customisation (I need to be able to set off-peak time ranges). Anyway, I've been having a little trouble researching these questions: Does Python have an API to monitor this?...

python - Monitoring a tcp port

For fun, I've been toying around with writing a load balancer in python and have been trying to figure the best (correct?) way to test if a port is available and the remote host is still there. I'm finding that, once connected, it becomes difficult to tell when the remote host goes down. I've turned keep alive on, but can't get it to recognize a downed connection sooner than a minute (I realize polling more often ...

monitoring - How do I watch a folder for changes and when changes are done using Python?

i need to watch a folder for incoming files. i did that with the following help: How do I watch a file for changes? the problem is that the files that are being moved are pretty big (10gb) and i want to be notified when all files are done moving. i tried comparing the size of the folder every 20 seconds but the f...

python - Monitoring files with twisted

Is it possible to write a Twisted application to monitor addtion of new files/folders to a specific folder? If yes, could someone point me to some code/link trying to do it, or give an idea how to do that?

python - is there anyway of monitoring the transfer rate of a urllib2 transfer?

This question already has answers here:

python - Monitoring Celery, what should I use?

Closed. This question does not meet Stack Overflow guid...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python