Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python how to quit screensaver

I have to quit from screensaver. I have an alarm clock (written in python-pygame) that after a minute, without input, goes in screensaver (blank). I have also a pir sensor that, when I pass my hand, it stops the alarm. Therefore, if I'm in blank screen, the screensaver is activated and the alarm sounds, I'm able to stop the alarm with the pir but the screen remains blank. so, I would ask if there is a command that I can put in the python script that can exit from screensaver when I move the hand near the pir. It also would be useful for looking at the time when the screensaver is activated. thanks

like image 952
user3072083 Avatar asked Sep 29 '26 02:09

user3072083


2 Answers

I used all the suggestions and I wrote the following code that it's really working:

if io.input(pir_pin):
    pygame.mouse.set_pos((random.choice(range(600)), random.choice(range(600))))
    alarm.just_die()
time.sleep(0.5)

when I move the hand I get the screen appear and, if the alarm is sounding, the alarm stops.

like image 165
user3072083 Avatar answered Sep 30 '26 14:09

user3072083


Assuming the program will keep running when the screensaver begins (it should, however, if it does not, look into creating a daemon or service that will run in the background even when the computer is asleep), there is a way to do this. My solution requires that the system is a windows system (which in all likelihood it is, though I would be more able to help if you had specified). This is exactly the kind of task the pywin32 library is built to perform. Accessing the wake-up code within the command line using subprocess or os modules can result in unexpected problems, so it seems that it may be best to generate a small movement of the mouse event to wake up the computer. Here is an example of code that will wake up a computer in screensaver:

import win32api
import random

def wakeup():
    win32api.SetCursorPos((random.choice(range(300)),random.choice(range(300))))

This will just move the mouse to a randomized location on the screen so that the computer wakes up.

As for checking the time, this is quite simple. Using the time module, you can get the system time with this command:

time.localtime(time.time())

Time.time() returns the time in seconds only, localtime will convert it into something easier to understand.

like image 28
trevorKirkby Avatar answered Sep 30 '26 14:09

trevorKirkby