1

I need to write a tool that simulates periodic mouse or keyboard presses on my mac.

The target is to simulate user activity, specifically in MS Teams, without interfering with normal use of the computer/programs. The non-interference is crucial here, i.e. a simulated key press muss be registered by the OS/apps as a key press but MUST NOT "enact" an actual interaction with antyhing (I read that this would be possible for Windows by simulating pressing a only theoretically existing, hence non-functional key F13).

Important is that this cannot be a application/program that is regularly installed, but should be a command line tool that is executed on system startup and runs in the background (like a cronjob).

How do I write something like this?

PS: How do I make the mouse click at current location using AppleScript? only has answers for mouse clicks.

7
  • You can only click in the frontmost app. That's going to restrict your options somewhat.
    – Tetsujin
    Commented Apr 6, 2023 at 10:26
  • Also stackoverflow.com/questions/4230867/…
    – Tetsujin
    Commented Apr 6, 2023 at 10:26
  • The linked question helps, but is not a duplicate of or answer to my question as it deals generally with programmatically clicking using AppleScript. It omits the question of recurrence and interference with other programs. I edited the question to emphasize on these aspects. Can we please reopen the question hence?
    – Xenonite
    Commented Apr 6, 2023 at 11:14
  • Such as adding a timer would be just a case of doing some research. As already said, you can only click into the frontmost app - so that's going to be your stumbling block. Every click is going to have to bring that app to the front & leave the click location accessible, unless [& you could try this out without scripting] the button or area you need to click will respond to Cmd/click without activating.
    – Tetsujin
    Commented Apr 6, 2023 at 11:17
  • 2
    This sounds like you’re trying to make it appear you’re online and working for work or school while you’re off elsewhere. You need this running in the shell because Teams can detect things like this in the GUI. Correct?
    – Allan
    Commented Apr 6, 2023 at 14:06

1 Answer 1

2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .