Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pynput Support #129

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ __pycache__/

# Fallback location used for the language model.
/model
/venv/
/package/python
!/package/python/readme.rst
!/package/python/setup.py
36 changes: 33 additions & 3 deletions nerd-dictation
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ USER_CONFIG = "nerd-dictation.py"

SIMULATE_INPUT_CODE_COMMAND = -1

try:
from pynput.keyboard import Key, Controller # type: ignore

keyboard = Controller()
TAP_DELAY = 0.012
except ImportError:
sys.stderr.write("Module 'pynput' is not installed. Defaulting input method to xdotool.")


# -----------------------------------------------------------------------------
# General Utilities
Expand Down Expand Up @@ -135,6 +143,24 @@ def execfile(filepath: str, mod: Optional[ModuleType] = None) -> Optional[Module
return mod


# -----------------------------------------------------------------------------
# Simulate Input: PYNPUT
#
def simulate_typing_with_pynput(delete_prev_chars: int, text: str) -> None:

# No setup/tear-down.
if delete_prev_chars == SIMULATE_INPUT_CODE_COMMAND:
return

if delete_prev_chars:
for _ in range(delete_prev_chars):
keyboard.tap(Key.backspace)
time.sleep(TAP_DELAY)
for c in text:
keyboard.type(c)
time.sleep(TAP_DELAY)


# -----------------------------------------------------------------------------
# Simulate Input: XDOTOOL
#
Expand Down Expand Up @@ -748,7 +774,8 @@ class from_words_to_digits:
continue

word_list[i:i_next] = [
("{:,d}".format(int(number)) if (numbers_use_separator and allow_reformat) else number) + suffix
("{:,d}".format(int(number)) if (numbers_use_separator and allow_reformat) else number)
+ suffix
]

if (i_number_prev != -1) and (i_number_prev + 1 != i):
Expand Down Expand Up @@ -1404,7 +1431,9 @@ def main_begin(
# Handled the resulting text
#
if output == "SIMULATE_INPUT":
if simulate_input_tool == "XDOTOOL":
if simulate_input_tool == "PYNPUT":
handle_fn = simulate_typing_with_pynput
elif simulate_input_tool == "XDOTOOL":
handle_fn = simulate_typing_with_xdotool
elif simulate_input_tool == "YDOTOOL":
handle_fn = simulate_typing_with_ydotool
Expand Down Expand Up @@ -1790,11 +1819,12 @@ def argparse_create_begin(subparsers: "argparse._SubParsersAction[argparse.Argum
"--simulate-input-tool",
dest="simulate_input_tool",
default="XDOTOOL",
choices=("XDOTOOL", "DOTOOL", "DOTOOLC", "YDOTOOL", "WTYPE", "STDOUT"),
choices=("PYNPUT", "XDOTOOL", "DOTOOL", "DOTOOLC", "YDOTOOL", "WTYPE", "STDOUT"),
metavar="SIMULATE_INPUT_TOOL",
help=(
"Program used to simulate keystrokes (default).\n"
"\n"
"- ``PYNPUT`` Compatible with all Linux distributions and Wayland.\n"
"- ``XDOTOOL`` Compatible with the X server only (default).\n"
"- ``DOTOOL`` Compatible with all Linux distributions and Wayland.\n"
"- ``DOTOOLC`` Same as DOTOOL but for use with the `dotoold` daemon.\n"
Expand Down
2 changes: 1 addition & 1 deletion package/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def main(base_dir):
packages=[""],
package_data={"": [NERD_DICTATION_DST, README_DST]},
include_package_data=True,
install_requires=["vosk"],
install_requires=["vosk", "pynput"],
python_requires=">=3.8",
)

Expand Down