Skip to content

Instantly share code, notes, and snippets.

@mickymots
Created July 22, 2021 18:48
Show Gist options
  • Save mickymots/00422555b00b86900255699320a66826 to your computer and use it in GitHub Desktop.
Save mickymots/00422555b00b86900255699320a66826 to your computer and use it in GitHub Desktop.
Inhibit
import pyautogui, sys
import time, random
import os
class WindowsInhibitor:
ES_CONTINUOUS = 0x80000000
ES_SYSTEM_REQUIRED = 0x00000001
def __init__(self):
pass
def inhibit(self):
import ctypes
print("Preventing Windows from going to sleep")
ctypes.windll.kernel32.SetThreadExecutionState(
WindowsInhibitor.ES_CONTINUOUS | \
WindowsInhibitor.ES_SYSTEM_REQUIRED)
def uninhibit(self):
import ctypes
print("Allowing Windows to go to sleep")
ctypes.windll.kernel32.SetThreadExecutionState(
WindowsInhibitor.ES_CONTINUOUS)
print('Determine how many seconds to wait before moving the cursor')
v = input()
print('Press Ctrl-C to quit.')
try:
if os.name == 'nt':
osSleep = WindowsInhibitor()
osSleep.inhibit()
while True:
x, y = pyautogui.position()
time.sleep(int(v))
a = random.randint(0, 1919) # assuming 1920px width
b = random.randint(0, 1079) # assuming 1080px height
pyautogui.moveTo(a, b, 4) # 2 is the time in seconds the cursor moves from x,y to a,b
except KeyboardInterrupt:
if osSleep:
osSleep.uninhibit()
print('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment