I managed to write a simple Python script for doing exactly what I needed:
Essentially, it presses the caps-lock-key (which needs to be disabled via system settings) in random intervals, therefore imitating user interaction and preventing your mac from going to sleep, the screensaver from starting, or MS teams from showing you as "away". At the same time, this doesn't show up as an installed program so it won't raise any suspicions.
The script comprises two files, one start-stop-script and one that is responsible for the key pressing. Both files go in the same directory:
pressy.py
import time
import random
from Quartz.CoreGraphics import CGEventCreateKeyboardEvent, CGEventPost, kCGEventKeyUp, kCGEventKeyDown
from Quartz import kCGHIDEventTap
def simulate_key_press(key_code, delay=0.05):
# Press the key
key_down_event = CGEventCreateKeyboardEvent(None, key_code, True)
CGEventPost(kCGHIDEventTap, key_down_event)
# Add a small delay to simulate a real key press
time.sleep(delay)
# Release the key
key_up_event = CGEventCreateKeyboardEvent(None, key_code, False)
CGEventPost(kCGHIDEventTap, key_up_event)
if __name__ == "__main__":
while True:
# 57 is caps_lock
key_code = 57
# Generate a random interval (in seconds) for the key presses
random_interval = random.uniform(10, 50)
# Simulate the key press
simulate_key_press(key_code)
# Wait for the random interval before the next key press
time.sleep(random_interval)
pressy
#!/usr/bin/env python3
import sys
import os
import subprocess
import signal
from pathlib import Path
pid_file = Path("pressy.pid")
def start():
if pid_file.exists():
print("Pressy is already running.")
return
script_path = Path(__file__).parent / "pressy.py"
process = subprocess.Popen([sys.executable, str(script_path)])
with open(pid_file, "w") as f:
f.write(str(process.pid))
print("Pressy has started.")
def stop():
if not pid_file.exists():
print("Pressy is not running.")
return
with open(pid_file, "r") as f:
pid = int(f.read())
try:
os.kill(pid, signal.SIGTERM)
pid_file.unlink()
print("Pressy has stopped.")
except ProcessLookupError:
print("Pressy process not found. Cleaning up...")
pid_file.unlink()
def status():
if pid_file.exists():
print("Pressy is running.")
else:
print("Pressy is not running.")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: ./pressy <start|stop|status>")
sys.exit(1)
command = sys.argv[1].lower()
if command == "start":
start()
elif command == "stop":
stop()
elif command == "status":
status()
else:
print("Invalid command. Usage: ./pressy <start|stop|status>")
Requirements:
- Python 3 (built and tested with Python 3.11)
Installation:
Use:
- Start: ./pressy start
- Stop: ./pressy stop
- Status: ./pressy status
Currently working on a small UI that allows to control everything with a set of buttons from the taskbar