Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can pyautogui be used to prevent windows screen lock?

I tried to use this script to prevent windows screen lock. The script works for moving the mouse, but it doesn't prevent windows 10 from locking.

import pyautogui
import time
import win32gui, win32con
import os

Minimize = win32gui.GetForegroundWindow()
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE)

x = 1

while x == 1:
    pyautogui.moveRel(1)
    pyautogui.moveRel(-1)
    time.sleep (300)
like image 758
Python account Avatar asked Oct 07 '19 16:10

Python account


People also ask

How do you stop Windows screen from locking?

Select “Administrative Templates” and then double-click “Control Panel.” Now, double-click “Personalization.” Double-click “Do Not Display the Lock Screen” and then select “Enabled” on the pop-up menu. Click “OK” when you're done.

How do I stop my office laptop from locking?

Right-click an area of the open Windows desktop, click “Personalize,” then click the “Screen Saver” icon. Click the “Change power settings” link in the Screen Saver Settings window. After the Power Options window opens, click the “Require a password on wakeup” link in the left pane.

Does python keep run when computer is locked?

You don't need to worry about the screen locking or turning off as these wont affect running processes. However, if your system is set to sleep after a set amount of time you may need to change this to Never. Keep in mind there are separate settings depending on whether or not the system is plugged in.

How do I lock my screen in Python?

If we type "rundll32.exe user32. dll, LockWorkStation" in the command prompt or powershell then it will lock the pc too.


1 Answers

Yes it can. But sadly not by moving mouse which I don't know why and would like to know. So, my suggestion is to use pyautogui KEYBOARD EVENTS if possible. I have solved my problems by using VOLUME-UP & VOLUME-DOWN keys. Example code is provided below:

import pyautogui
import time

while True:
    pyautogui.press('volumedown')
    time.sleep(1)
    pyautogui.press('volumeup')
    time.sleep(5)

You can use any other keys if you want.

like image 85
Shahriar Rahman Zahin Avatar answered Sep 23 '22 22:09

Shahriar Rahman Zahin