Post

Python Keep Awake for Teams

This guide explains how to use the keep_awake.py script to prevent your Windows computer from going to sleep by simulating user activity. The script performs periodic mouse movements and clicks to keep the system awake.

Prerequisites

  • Python installed on your computer. You can download and install Python from python.org.
  • pyautogui library installed. You can install it using pip.

Step-by-Step Instructions

1. Install **pyautogui** Library: Open your command prompt (CMD) and run the following command to install the pyautogui library:

1
pip install pyautogui

2. Create the Script: Save the following Python script as keep_awake.py on your computer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pyautogui
import time
from datetime import datetime
import ctypes

# Constants for time checks
START_TIME = "07:30"
END_TIME = "17:00"

# Function to get system idle time in seconds
def get_idle_time():
    class LASTINPUTINFO(ctypes.Structure):
        _fields_ = [("cbSize", ctypes.c_uint), ("dwTime", ctypes.c_uint)]

    lastInputInfo = LASTINPUTINFO()
    lastInputInfo.cbSize = ctypes.sizeof(LASTINPUTINFO)
    if ctypes.windll.user32.GetLastInputInfo(ctypes.byref(lastInputInfo)):
        millis = ctypes.windll.kernel32.GetTickCount() - lastInputInfo.dwTime
        return millis / 1000.0
    else:
        return 0

# Function to check if current time is within the specified hours
def within_active_hours():
    now = datetime.now()
    current_time = now.strftime("%H:%M")
    day_of_week = now.weekday()  # Monday is 0 and Sunday is 6
    return day_of_week < 5 and START_TIME <= current_time <= END_TIME

# Function to perform mouse move and click
def keep_awake():
    pyautogui.moveRel(0, 10)  # Move mouse by 10 pixels down
    pyautogui.moveRel(0, -10)  # Move mouse back to original position
    pyautogui.click()  # Perform a mouse click

# Main loop to check idle time and perform actions if necessary
while True:
    if within_active_hours():
        idle_time = get_idle_time()
        if idle_time > 60:
            keep_awake()
    time.sleep(30)  # Check every 30 seconds

3. Run the Script: Open your command prompt, navigate to the directory where you saved keep_awake.py, and run the script with the following command:

1
python keep_awake.py

The script will now run in an infinite loop, moving the mouse slightly and clicking every 60 seconds. This activity will prevent your computer from going to sleep.

Stop the Script: To stop the script, simply press Ctrl + C in the command prompt where the script is running. This will terminate the script and return control to you.

Customization

  • Interval Adjustment: By default, the script performs actions every 60 seconds. You can adjust this interval by changing the interval parameter in the keep_awake() function call:
1
2
if __name__ == "__main__":
    keep_awake(interval=30)  # Actions every 30 seconds

Troubleshooting

Script Not Running: Ensure you have Python installed and the pyautogui library properly installed. Verify the script file (keep_awake.py) is in the correct directory and you are using the correct command to run it.

Permission Issues: Running automation scripts may require administrative privileges. Ensure you run the command prompt as an administrator if you encounter permission issues.

By following these steps, you can use the keep_awake.py script to keep your Windows computer awake by simulating user activity.

This post is licensed under CC BY 4.0 by the author.