Skip to content

Instantly share code, notes, and snippets.

@smeech
Last active November 12, 2025 10:55
Show Gist options
  • Select an option

  • Save smeech/6638d272ad0a40f1a3c7803f8848bb49 to your computer and use it in GitHub Desktop.

Select an option

Save smeech/6638d272ad0a40f1a3c7803f8848bb49 to your computer and use it in GitHub Desktop.
[Delays in replacements] Espanso doesn't support replacements which include delays. A solution is to ignore Espanso's own replace and use another tool to inject characters. Similar methods are available for Windows PowerShell using e.g.: (New-Object -ComObject wscript.shell).SendKeys #espanso #python
# Espanso trigger to be used in conjunction with the script test_pynput.py
# Uses the Python pynput library to inject text, instead of Espanso. Enables the
# addition of pauses (sleep) and <Tab> etc. keys and can include Espanso {{variables}}
# See https://pynput.readthedocs.io/en/latest/keyboard.html#controlling-the-keyboard
# Supports type, tap, press, release, and sleep
# Whilst you can add to the replace:, anything there will be injected AFTER the script's
# output
- trigger: :delay
replace: '{{output}}'
vars:
- name: trig
type: echo
params:
echo: :delay # This must match the trigger text
- name: input
type: echo
params:
echo: | # Amend the contents below to suit, adding variables etc.
type Hello, World!
tap enter
type Pausing for one second
tap enter
sleep 1
type How are you?
tap space
press shift
type I am a bot
release shift
tap enter
type The trigger was {{trig}}
tap enter
- name: output
type: script
params:
args:
- python
- '%CONFIG%/scripts/test_pynput.py'
- '{{trig}}'
- '{{input}}'
# Place this script "test_pynput.py" in your espanso/scripts directory
# Intended for use with the "Delays in replacements, generic" yml file.
# https://pastebin.com/edit/c4QHdnc8
# Uses the Python pynput library to inject text, instead of Espanso. Enables the
# addition of pauses (sleep) and <Tab> etc. keys and can include Espanso {{variables}}
# See https://pynput.readthedocs.io/en/latest/keyboard.html#controlling-the-keyboard
# Supports type, tap, press, release, and sleep
# Place this script "test_pynput.py" in your espanso/scripts directory
import argparse, time
from pynput.keyboard import Controller, Key
# Initialize the keyboard controller
keyboard = Controller()
def parse_and_execute_commands(commands):
lines = commands.strip().splitlines()
for line in lines:
line = line.strip()
if line.startswith('type'):
text = line[len('type '):].strip()
keyboard.type(text)
elif line.startswith('tap'):
key_name = line[len('tap '):].strip().lower()
key = getattr(Key, key_name, key_name)
keyboard.tap(key)
elif line.startswith('press'):
key_name = line[len('press '):].strip().lower()
key = getattr(Key, key_name, key_name)
keyboard.press(key)
elif line.startswith('release'):
key_name = line[len('release '):].strip().lower()
key = getattr(Key, key_name, key_name)
keyboard.release(key)
elif line.startswith('sleep'):
time_to_sleep = float(line[len('sleep '):].strip())
time.sleep(time_to_sleep)
def main():
# Set up argument parsing
parser = argparse.ArgumentParser(description="Execute keyboard automation commands.")
parser.add_argument('trig', type=str, help='The trigger key to simulate.')
parser.add_argument('input', type=str, help='The input commands to execute.')
args = parser.parse_args()
# Press backspace key as many times as the length of trig
for _ in args.trig: keyboard.tap(Key.backspace)
# Parse and execute the input commands
if args.input:
parse_and_execute_commands(args.input)
else:
print("No input provided.")
# Replace trigger for Espanso to remove after the script
keyboard.type(args.trig)
if __name__ == "__main__":
main()
@smeech

smeech commented Jan 31, 2025

Copy link
Copy Markdown
Author

This is now available as an Espanso package.
Install with espanso install delays-characters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment