Prevent your computer from locking : Python to simulate mouse movements

Learn Python @ Freshers.in

You can use Python to simulate mouse movements and prevent your computer from locking. Here’s a simple script using the pyautogui library that moves the mouse slightly every few minutes to keep your PC active:

First, you need to install the pyautogui library. You can do this by running:

pip install pyautogui

Then, you can use the following script to move the mouse slightly every few minutes:

import pyautogui
import time

def move_mouse():
    while True:
        current_pos = pyautogui.position()
        pyautogui.moveTo(current_pos[0] + 1, current_pos[1] + 1)
        time.sleep(0.5)
        pyautogui.moveTo(current_pos)
        time.sleep(300)  # wait for 5 minutes (300 seconds)

if __name__ == "__main__":
    move_mouse()

This script will:

  1. Get the current mouse position.
  2. Move the mouse by 1 pixel in both x and y directions.
  3. Wait for half a second and move it back to the original position.
  4. Wait for 5 minutes before repeating the process.

Run this script in the background, and it should prevent your PC from locking due to inactivity. Note that you might need to adjust the sleep time depending on your specific needs.

Learn Python Programming

Refer more on python Article here :

Author: user