Advertisement
smeech

Generic Espanso/Python code to inject delays, tabs and text etc.

Aug 23rd, 2024 (edited)
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 1.38 KB | None | 0 0
  1.   #  Snippet to inject text using Python. See:
  2.   #  https://pynput.readthedocs.io/en/latest/keyboard.html#controlling-the-keyboard
  3.   - trigger: :test
  4.     replace: "{{output}}" # Redundant placeholder
  5.     vars:
  6.       - name: output
  7.         type: script
  8.         params:
  9.           args:
  10.            - python
  11.             - -c
  12.             - |
  13.              import time
  14.               from pynput.keyboard import Controller, Key
  15.               keyboard = Controller()
  16.  
  17.               trigger = ":test" # Match the trigger string length
  18.              
  19.               # Helper function to press a key multiple times.
  20.               def press_key(key, times=1): [keyboard.tap(key) for _ in range(times)]
  21.  
  22.               # Remove trigger characters using the function
  23.               press_key(Key.backspace, times=len(trigger))
  24.  
  25.               # Type a message followed by a newline
  26.               keyboard.type("Replace this with text and variables")
  27.               keyboard.tap(Key.enter)
  28.              
  29.               time.sleep(1.5) # Wait for 1.5 seconds
  30.  
  31.               # Press the Tab key twice and then press the Space key
  32.               press_key(Key.tab, times=2)
  33.               keyboard.tap(Key.space)
  34.  
  35.               # Type some more text
  36.               keyboard.type("Some more text here")
  37.  
  38.               # Supply dummy characters for Espanso to remove
  39.               keyboard.type(trigger)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement