From 43fce3ee243ba90c0c76cf05d1d727d598b45b7f Mon Sep 17 00:00:00 2001 From: smeech Date: Tue, 14 Jan 2025 17:16:01 +0000 Subject: [PATCH 1/5] delays-characters package An Espanso trigger that uses the Python `pynput` library to inject text, *instead* of Espanso. This enables the addition of pauses (sleep), \ etc, and other key combinations not supported by Espanso. --- packages/delays-characters/0.1.0/README.md | 13 ++++ .../delays-characters/0.1.0/_manifest.yml | 7 +++ packages/delays-characters/0.1.0/package.yml | 41 +++++++++++++ .../delays-characters/0.1.0/parse_pynput.py | 61 +++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 packages/delays-characters/0.1.0/README.md create mode 100644 packages/delays-characters/0.1.0/_manifest.yml create mode 100644 packages/delays-characters/0.1.0/package.yml create mode 100644 packages/delays-characters/0.1.0/parse_pynput.py diff --git a/packages/delays-characters/0.1.0/README.md b/packages/delays-characters/0.1.0/README.md new file mode 100644 index 0000000..b2ed7c6 --- /dev/null +++ b/packages/delays-characters/0.1.0/README.md @@ -0,0 +1,13 @@ +# Delays-characters + +An Espanso trigger that uses the Python `pynput` library to inject text, *instead* of Espanso. This enables the addition of pauses (sleep), \ etc, and other key combinations not supported by Espanso. It can include use of Espanso {{variables}}. + +See https://pynput.readthedocs.io/en/latest/keyboard.html#controlling-the-keyboard for details of the keywords, and https://pynput.readthedocs.io/en/latest/keyboard.html#pynput.keyboard.Key for the key names. + +Supports keywords "type", "tap", "press", "release", and "sleep". + +The package includes a sample script which demonstrates a delay and the effect of simulating pressing the \ key. For different scripts, copy, and rename, the `package.yml` file into the `espanso/match/` directory. Edit the trigger value and Input list to suit your purpose. + +NB. *The variable {{Trig}} **must** match the trigger in length at least, so that `parse_pynput.py` removes the trigger text cleanly.* + +A possible future enhancement could be the addition of mouse control. \ No newline at end of file diff --git a/packages/delays-characters/0.1.0/_manifest.yml b/packages/delays-characters/0.1.0/_manifest.yml new file mode 100644 index 0000000..d8706a5 --- /dev/null +++ b/packages/delays-characters/0.1.0/_manifest.yml @@ -0,0 +1,7 @@ +name: delays-characters +title: Delays and characters +description: A package to allow delays, and characters not supported by Espanso, to be injected using the Python pynput library. +homepage: https://github.com/smeech +version: 0.1.0 +author: Stephen Meech +tags: [delays, characters, keyboard, control, python] \ No newline at end of file diff --git a/packages/delays-characters/0.1.0/package.yml b/packages/delays-characters/0.1.0/package.yml new file mode 100644 index 0000000..1fde446 --- /dev/null +++ b/packages/delays-characters/0.1.0/package.yml @@ -0,0 +1,41 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/espanso/espanso/dev/schemas/match.schema.json + +# Supports type, tap, press, release, and sleep. + +# For new scripts this file may be copied to the espanso/match directory and edited. + +matches: + - trigger: :delay # Remember to include this value in Trig, below. + 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 + tap tab + type The End. + - name: Output + type: script + params: + args: + - python + - '%CONFIG%/match/packages/delays-characters/parse_pynput.py' + - '{{Trig}}' + - '{{Input}}' \ No newline at end of file diff --git a/packages/delays-characters/0.1.0/parse_pynput.py b/packages/delays-characters/0.1.0/parse_pynput.py new file mode 100644 index 0000000..61cbe61 --- /dev/null +++ b/packages/delays-characters/0.1.0/parse_pynput.py @@ -0,0 +1,61 @@ +# Intended for use with the delays-characters package.yml file. +# Uses the Python pynput library to inject text, instead of Espanso. Enables the +# addition of pauses (sleep) and 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 + +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 the trigger + 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() From bc6c5599ba3d3ee20f71d323867440ca89a85d75 Mon Sep 17 00:00:00 2001 From: Stephen Meech Date: Tue, 14 Jan 2025 17:26:40 +0000 Subject: [PATCH 2/5] Update README.md Added pynput installation instructions. --- packages/delays-characters/0.1.0/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/delays-characters/0.1.0/README.md b/packages/delays-characters/0.1.0/README.md index b2ed7c6..1998d4d 100644 --- a/packages/delays-characters/0.1.0/README.md +++ b/packages/delays-characters/0.1.0/README.md @@ -4,10 +4,12 @@ An Espanso trigger that uses the Python `pynput` library to inject text, *instea See https://pynput.readthedocs.io/en/latest/keyboard.html#controlling-the-keyboard for details of the keywords, and https://pynput.readthedocs.io/en/latest/keyboard.html#pynput.keyboard.Key for the key names. +If necessary, use `python3 -m pip install pynput` to add pynput to your Python installation. + Supports keywords "type", "tap", "press", "release", and "sleep". The package includes a sample script which demonstrates a delay and the effect of simulating pressing the \ key. For different scripts, copy, and rename, the `package.yml` file into the `espanso/match/` directory. Edit the trigger value and Input list to suit your purpose. NB. *The variable {{Trig}} **must** match the trigger in length at least, so that `parse_pynput.py` removes the trigger text cleanly.* -A possible future enhancement could be the addition of mouse control. \ No newline at end of file +A possible future enhancement could be the addition of mouse control. From a92a19a1a09332207228a1458f59a3998fa735ba Mon Sep 17 00:00:00 2001 From: Stephen Meech Date: Tue, 14 Jan 2025 17:28:08 +0000 Subject: [PATCH 3/5] Update README.md --- packages/delays-characters/0.1.0/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/delays-characters/0.1.0/README.md b/packages/delays-characters/0.1.0/README.md index 1998d4d..b7494de 100644 --- a/packages/delays-characters/0.1.0/README.md +++ b/packages/delays-characters/0.1.0/README.md @@ -4,7 +4,7 @@ An Espanso trigger that uses the Python `pynput` library to inject text, *instea See https://pynput.readthedocs.io/en/latest/keyboard.html#controlling-the-keyboard for details of the keywords, and https://pynput.readthedocs.io/en/latest/keyboard.html#pynput.keyboard.Key for the key names. -If necessary, use `python3 -m pip install pynput` to add pynput to your Python installation. +If necessary, use `python -m pip install pynput` to add pynput to your Python installation. Supports keywords "type", "tap", "press", "release", and "sleep". From 35962a4313cbe27f11feafb486551e98789521cb Mon Sep 17 00:00:00 2001 From: smeech Date: Tue, 14 Jan 2025 23:03:31 +0000 Subject: [PATCH 4/5] Added testing for 'pynput' and generating an error message to Espanso log if not present. README.md updated to mention Python version. --- packages/delays-characters/0.1.0/README.md | 2 +- packages/delays-characters/0.1.0/parse_pynput.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/delays-characters/0.1.0/README.md b/packages/delays-characters/0.1.0/README.md index b7494de..ed22da3 100644 --- a/packages/delays-characters/0.1.0/README.md +++ b/packages/delays-characters/0.1.0/README.md @@ -4,7 +4,7 @@ An Espanso trigger that uses the Python `pynput` library to inject text, *instea See https://pynput.readthedocs.io/en/latest/keyboard.html#controlling-the-keyboard for details of the keywords, and https://pynput.readthedocs.io/en/latest/keyboard.html#pynput.keyboard.Key for the key names. -If necessary, use `python -m pip install pynput` to add pynput to your Python installation. +If necessary, use `python -m pip install pynput` to add pynput to your Python installation. Tested here with Python 3 but may work from Python 2.7 or earlier. Supports keywords "type", "tap", "press", "release", and "sleep". diff --git a/packages/delays-characters/0.1.0/parse_pynput.py b/packages/delays-characters/0.1.0/parse_pynput.py index 61cbe61..809ee8d 100644 --- a/packages/delays-characters/0.1.0/parse_pynput.py +++ b/packages/delays-characters/0.1.0/parse_pynput.py @@ -5,7 +5,14 @@ # Supports type, tap, press, release, and sleep import argparse, time -from pynput.keyboard import Controller, Key +try: + from pynput.keyboard import Controller, Key +except ImportError: + import sys + print("Error: The 'pynput' library is not installed.", file=sys.stderr) + print("Install it using: pip install pynput", file=sys.stderr) + sys.exit(1) # Exit with a non-zero status code to indicate failure + # Initialize the keyboard controller keyboard = Controller() From 88516026eefbb45b7faf2142f9a03671174c99b3 Mon Sep 17 00:00:00 2001 From: Stephen Meech Date: Tue, 14 Jan 2025 23:14:17 +0000 Subject: [PATCH 5/5] Update README.md --- packages/delays-characters/0.1.0/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/delays-characters/0.1.0/README.md b/packages/delays-characters/0.1.0/README.md index ed22da3..dc8fdf3 100644 --- a/packages/delays-characters/0.1.0/README.md +++ b/packages/delays-characters/0.1.0/README.md @@ -4,7 +4,7 @@ An Espanso trigger that uses the Python `pynput` library to inject text, *instea See https://pynput.readthedocs.io/en/latest/keyboard.html#controlling-the-keyboard for details of the keywords, and https://pynput.readthedocs.io/en/latest/keyboard.html#pynput.keyboard.Key for the key names. -If necessary, use `python -m pip install pynput` to add pynput to your Python installation. Tested here with Python 3 but may work from Python 2.7 or earlier. +If necessary, use `python -m pip install pynput` to add pynput to your Python installation. Tested here with Python 3.10 but may work from Python 2.7 or earlier. Supports keywords "type", "tap", "press", "release", and "sleep".