Advertisement
smeech

test_pynput.py

Aug 23rd, 2024 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. # Place this script "test_pynput.py" in your espanso/scripts directory
  2. # Intended for use with the "Delays in replacements, generic" yml file.
  3. # https://pastebin.com/edit/c4QHdnc8
  4. # Uses the Python pynput library to inject text, instead of Espanso. Enables the
  5. # addition of pauses (sleep) and <Tab> etc. keys and can include Espanso {{variables}}
  6. # See https://pynput.readthedocs.io/en/latest/keyboard.html#controlling-the-keyboard
  7. # Supports type, tap, press, release, and sleep
  8.  
  9. # Place this script "test_pynput.py" in your espanso/scripts directory
  10.  
  11. import argparse, time
  12. from pynput.keyboard import Controller, Key
  13.  
  14. # Initialize the keyboard controller
  15. keyboard = Controller()
  16.  
  17. def parse_and_execute_commands(commands):
  18.     lines = commands.strip().splitlines()
  19.  
  20.     for line in lines:
  21.         line = line.strip()
  22.         if line.startswith('type'):
  23.             text = line[len('type '):].strip()
  24.             keyboard.type(text)
  25.        
  26.         elif line.startswith('tap'):
  27.             key_name = line[len('tap '):].strip().lower()
  28.             key = getattr(Key, key_name, key_name)
  29.             keyboard.tap(key)
  30.        
  31.         elif line.startswith('press'):
  32.             key_name = line[len('press '):].strip().lower()
  33.             key = getattr(Key, key_name, key_name)
  34.             keyboard.press(key)
  35.  
  36.         elif line.startswith('release'):
  37.             key_name = line[len('release '):].strip().lower()
  38.             key = getattr(Key, key_name, key_name)
  39.             keyboard.release(key)
  40.        
  41.         elif line.startswith('sleep'):
  42.             time_to_sleep = float(line[len('sleep '):].strip())
  43.             time.sleep(time_to_sleep)
  44.  
  45. def main():
  46.     # Set up argument parsing
  47.     parser = argparse.ArgumentParser(description="Execute keyboard automation commands.")
  48.     parser.add_argument('trig', type=str, help='The trigger key to simulate.')
  49.     parser.add_argument('input', type=str, help='The input commands to execute.')
  50.     args = parser.parse_args()
  51.  
  52.     # Press backspace key as many times as the length of trig
  53.     for _ in args.trig: keyboard.tap(Key.backspace)
  54.  
  55.     # Parse and execute the input commands
  56.     if args.input:
  57.         parse_and_execute_commands(args.input)
  58.     else:
  59.         print("No input provided.")
  60.  
  61.     # Replace trigger for Espanso to remove after the script
  62.     keyboard.type(args.trig)
  63.  
  64. if __name__ == "__main__":
  65.     main()
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement