From 02af2f8225918c14fa1c01888e956828ab6c18db Mon Sep 17 00:00:00 2001 From: John Gebbie Date: Thu, 3 Nov 2022 11:30:31 +0000 Subject: [PATCH] Add support for dotool --- changelog.rst | 1 + nerd-dictation | 33 +++++++++++++++++++++++++-------- readme.rst | 6 ++++-- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/changelog.rst b/changelog.rst index c62e149..22c8c74 100644 --- a/changelog.rst +++ b/changelog.rst @@ -3,6 +3,7 @@ Changelog ######### +- 2022/11/03: Add ``dotool`` support with ``--simpulate-input-tool=DOTOOL``. - 2022/06/05: Add packaging script for PIP/setup-tools to optionally install via PIP. - 2022/05/16: Add ``ydotool`` support with ``--simpulate-input-tool=YDOTOOL``. - 2022/04/18: Add ``--input`` option to specify audio recording command, added ``sox`` input. diff --git a/nerd-dictation b/nerd-dictation index 9d161a9..93e3412 100755 --- a/nerd-dictation +++ b/nerd-dictation @@ -1061,13 +1061,29 @@ def main_begin( # if output == "SIMULATE_INPUT": - def handle_fn(text: str, delete_prev_chars: int) -> None: - if delete_prev_chars: - # Backspace keycode. In ydotool we need to mark the key pressed with :1, then release it with :0 - # The default delay between keypresses is long and key delay stands for one key state change, - # so 3 ms == 6 ms to press and release backspace once. - simulate_backspace_presses(delete_prev_chars, simulate_input_tool) - simulate_typing(text, simulate_input_tool) + if simulate_input_tool == "DOTOOL": + dotool = subprocess.Popen("dotool", stdin=subprocess.PIPE, text=True) + assert dotool.stdin is not None + dotool.stdin.write("keydelay 4\ntypedelay 12\n") + dotool.stdin.flush() + + def handle_fn(text: str, delete_prev_chars: int) -> None: + assert dotool.stdin is not None + if delete_prev_chars: + dotool.stdin.write("key" + " backspace" * delete_prev_chars + "\n") + dotool.stdin.flush() + dotool.stdin.write("type " + text + "\n") + dotool.stdin.flush() + + else: + + def handle_fn(text: str, delete_prev_chars: int) -> None: + if delete_prev_chars: + # Backspace keycode. In ydotool we need to mark the key pressed with :1, then release it with :0 + # The default delay between keypresses is long and key delay stands for one key state change, + # so 3 ms == 6 ms to press and release backspace once. + simulate_backspace_presses(delete_prev_chars, simulate_input_tool) + simulate_typing(text, simulate_input_tool) elif output == "STDOUT": @@ -1344,12 +1360,13 @@ def argparse_create_begin(subparsers: argparse._SubParsersAction) -> None: "--simulate-input-tool", dest="simulate_input_tool", default="XDOTOOL", - choices=("XDOTOOL", "YDOTOOL", "WTYPE"), + choices=("XDOTOOL", "DOTOOL", "YDOTOOL", "WTYPE"), metavar="SIMULATE_INPUT_TOOL", help=( "Program used to simulate keystrokes (default).\n" "\n" "- ``XDOTOOL`` Compatible with the X server only (default).\n" + "- ``DOTOOL`` Compatible with all Linux distributions and Wayland.\n" "- ``YDOTOOL`` Compatible with all Linux distributions and Wayland but requires some setup.\n" "- ``WTYPE`` Compatible with Wayland.\n" " For help on setting up ydotool, see ``readme-ydotool.rst`` in the nerd-dictation repository.\n" diff --git a/readme.rst b/readme.rst index 88de9fa..95fc152 100644 --- a/readme.rst +++ b/readme.rst @@ -70,8 +70,9 @@ Dependencies - ``parec`` command for recording from pulse-audio (default) or ``sox`` command as alternative. See the guide: `Using sox with nerd-dictation `_. -- ``xdotool`` (default, X11 only) or - ``ydotool`` command to simulate keyboard input (supports X11 & Wayland). +- ``xdotool`` command to simulate input in X11 (default) or + ``dotool`` command to simulate input anywhere (X11/Wayland/TTYs) or + ``ydotool`` similar to dotool but requires setup and a daemon to be running. See the setup guide: `Using ydotool with nerd-dictation `_ or ``wtype`` an "xdotool for wayland" @@ -243,6 +244,7 @@ options: Program used to simulate keystrokes (default). - ``XDOTOOL`` Compatible with the X server only (default). + - ``DOTOOL`` Compatible with all Linux distributions and Wayland. - ``YDOTOOL`` Compatible with all Linux distributions and Wayland but requires some setup. - ``WTYPE`` Compatible with Wayland. For help on setting up ydotool, see ``readme-ydotool.rst`` in the nerd-dictation repository.