From a9934be6d3faa54f65335241b2232e0a1e44bcbe Mon Sep 17 00:00:00 2001 From: Shoban Chiddarth Date: Sat, 9 Dec 2023 09:44:46 +0530 Subject: [PATCH 1/5] Add time `interval` for typing in `Controller().type()` Now each key press+release will have `interval` amount of time in between them --- lib/pynput/keyboard/_base.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pynput/keyboard/_base.py b/lib/pynput/keyboard/_base.py index 71a00ee..fc255cb 100644 --- a/lib/pynput/keyboard/_base.py +++ b/lib/pynput/keyboard/_base.py @@ -27,6 +27,7 @@ import contextlib import enum import threading +import time import unicodedata import six @@ -473,7 +474,7 @@ def pressed(self, *args): for key in reversed(args): self.release(key) - def type(self, string): + def type(self, string, interval=0): """Types a string. This method will send all key presses and releases necessary to type @@ -490,6 +491,7 @@ def type(self, string): try: self.press(key) self.release(key) + time.sleep(interval) except (ValueError, self.InvalidKeyException): raise self.InvalidCharacterException(i, character) From 07981809247880d8f010ec13d892ef7a799bd0ee Mon Sep 17 00:00:00 2001 From: Shoban Chiddarth Date: Sat, 9 Dec 2023 09:49:37 +0530 Subject: [PATCH 2/5] Updated the docs for the `interval` functionality of `pynput.keyboard.Controller().type()` --- docs/keyboard-usage.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/keyboard-usage.rst b/docs/keyboard-usage.rst index caec295..57407ae 100644 --- a/docs/keyboard-usage.rst +++ b/docs/keyboard-usage.rst @@ -26,6 +26,9 @@ Use ``pynput.keyboard.Controller`` like this:: # Type 'Hello World' using the shortcut type method keyboard.type('Hello World') + # To add an interval between each key press and release while typing, provide the interval time in seconds like this + keyboard.type('Hello World', interval=0.05) + Monitoring the keyboard ----------------------- From 0dc615e19238d62016396a6ccd233f4703bdf9da Mon Sep 17 00:00:00 2001 From: Shoban Chiddarth Date: Wed, 17 Jul 2024 21:18:04 +0530 Subject: [PATCH 3/5] Added `hold` argument to `type()` function It will hold each key for `hold` seconds. --- lib/pynput/keyboard/_base.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pynput/keyboard/_base.py b/lib/pynput/keyboard/_base.py index fc255cb..5df3a98 100644 --- a/lib/pynput/keyboard/_base.py +++ b/lib/pynput/keyboard/_base.py @@ -474,7 +474,7 @@ def pressed(self, *args): for key in reversed(args): self.release(key) - def type(self, string, interval=0): + def type(self, string, interval=0, hold=0): """Types a string. This method will send all key presses and releases necessary to type @@ -490,6 +490,7 @@ def type(self, string, interval=0): key = _CONTROL_CODES.get(character, character) try: self.press(key) + time.sleep(hold) self.release(key) time.sleep(interval) From f5a5ca8075f33ecc340896a4db464fdea48e6623 Mon Sep 17 00:00:00 2001 From: Shoban Chiddarth Date: Wed, 17 Jul 2024 21:25:06 +0530 Subject: [PATCH 4/5] Updated comments and documentation reg 0dc615e --- docs/keyboard-usage.rst | 3 +++ lib/pynput/keyboard/_base.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/docs/keyboard-usage.rst b/docs/keyboard-usage.rst index 57407ae..bc4c715 100644 --- a/docs/keyboard-usage.rst +++ b/docs/keyboard-usage.rst @@ -29,6 +29,9 @@ Use ``pynput.keyboard.Controller`` like this:: # To add an interval between each key press and release while typing, provide the interval time in seconds like this keyboard.type('Hello World', interval=0.05) + # To hold each key for n seconds, provide the seconds n as float like this + keyboard.type('Hello World', hold=0.2) + Monitoring the keyboard ----------------------- diff --git a/lib/pynput/keyboard/_base.py b/lib/pynput/keyboard/_base.py index 5df3a98..b24390a 100644 --- a/lib/pynput/keyboard/_base.py +++ b/lib/pynput/keyboard/_base.py @@ -482,6 +482,10 @@ def type(self, string, interval=0, hold=0): :param str string: The string to type. + :param float interval: delay between key presses + + :param float hold: seconds that each key will be held down + :raises InvalidCharacterException: if an untypable character is encountered """ From 22251c551d646c07202ed4db0d8156b71395ac21 Mon Sep 17 00:00:00 2001 From: Shoban Chiddarth Date: Mon, 5 May 2025 18:05:56 +0530 Subject: [PATCH 5/5] Added tests for `hold` and `interval` Regarding a9934be and 0dc615e --- tests/keyboard_controller_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/keyboard_controller_tests.py b/tests/keyboard_controller_tests.py index e1e0ad6..5d36d33 100644 --- a/tests/keyboard_controller_tests.py +++ b/tests/keyboard_controller_tests.py @@ -89,7 +89,7 @@ def assert_input(self, failure_message, expected): :param text: The text to type and expect. """ with self.capture() as collect: - self.controller.type(expected) + self.controller.type(expected, interval = 0, hold = 0) self.assertIn(expected, collect(), failure_message)