Skip to content

Instantly share code, notes, and snippets.

@Multihuntr
Last active July 4, 2025 09:13
Show Gist options
  • Save Multihuntr/7c56c91c1798f0c71706cd2a533bd73d to your computer and use it in GitHub Desktop.
Save Multihuntr/7c56c91c1798f0c71706cd2a533bd73d to your computer and use it in GitHub Desktop.
Macro recording/playback in OS (across all applications)

What is it?

It's for repetitive tasks that are too small to be bothered writing a macro script into something like AutoHotKey, but annoying and repetitive enough that you want some automation.

It allows you to record some keypresses, and then replay those keypresses, using keyboard shortcuts. It's like the clipboard, but for keypresses instead of text. Recordings do not persist across restarts/logins, and are overwritten when you record a new macro. (Does not actually interact with the clipboard at all)

Setup

  1. Add keyboard shortcuts to trigger the record.py and replay.py scripts. They work globally on the OS. I use ctrl+super+e for record and super+e for replay.
  2. Install keyboard python library.
  3. Change the authkey in all files (just to be safe)

Do this once, ever.

Launch

Start server.py as super user. e.g. sudo python server.py

Do this every time you start your system. Add to startup applications to be fancy. I used this guide to add it to systemd.

Use

  1. Press the record shortcut (ctrl+super+e).
  2. Do the task like normal.
  3. Press esc to stop recording.
  4. Press the replay shortcut (super+e) as many times as you need.

Caveats

  1. You have to release the modifier key of the triggering key chord before the macro will execute. Make sure to not press any modifier keys while it is running, else the key presses of the macro will be triggered with that modifier key. E.g. if your macro includes the 'q' key, and you accidentally hold ctrl while it plays back, it will close your window.
  2. It defaults to replaying as fast as it can. If you need a timed playback, modify line 24 of the server; change the 0 to something like 2.0 (for double-speed playback) and restart the server. (see: https://github.com/boppreh/keyboard?tab=readme-ov-file#keyboard.play)
# Set this file to be triggered by keyboard shortcut. I use ctrl+super+e
from multiprocessing.connection import Client
conn = Client(("localhost", 6999), authkey=b"ghdjktufhgvn")
conn.send(0)
conn.close()
# Set this file to be triggered by keyboard shortcut. I use super+e
from multiprocessing.connection import Client
conn = Client(("localhost", 6999), authkey=b"ghdjktufhgvn")
conn.send(1)
conn.close()
from multiprocessing.connection import Listener
import time
import keyboard
def read_keys():
events = keyboard.record(until="esc")
# Filter out keys pressed for starting recording
while events[0].event_type == "up":
events = events[1:]
# Filter out the final esc press
while events[-1].name == "esc":
events = events[:-1]
return events
def write_keys(events):
# `keyboard` was thinking my super keys were 'alt', so I'm using their specific key codes
# See: https://sites.uclouvain.be/SystInfo/usr/include/linux/input.h.html
for key in ["shift", "alt", "ctrl", 125, 126]:
while keyboard.is_pressed(key):
time.sleep(0.01)
keyboard.play(events, 0)
server = Listener(("localhost", 6999), authkey=b"3MtbUixog6ETh")
conn = server.accept()
events = None
while True:
msg = conn.recv()
if msg == 0:
# print("You asked to record")
events = read_keys()
# print("Done recording")
if msg == 1 and events is not None:
# print("You asked to play")
write_keys(events)
conn = server.accept()
server.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment