Skip to content
6 changes: 6 additions & 0 deletions docs/keyboard-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ 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)

# 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
-----------------------
Expand Down
9 changes: 8 additions & 1 deletion lib/pynput/keyboard/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import contextlib
import enum
import threading
import time
import unicodedata

import six
Expand Down Expand Up @@ -473,14 +474,18 @@ def pressed(self, *args):
for key in reversed(args):
self.release(key)

def type(self, string):
def type(self, string, interval=0, hold=0):
"""Types a string.

This method will send all key presses and releases necessary to type
all characters in the string.

: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
"""
Expand All @@ -489,7 +494,9 @@ def type(self, string):
key = _CONTROL_CODES.get(character, character)
try:
self.press(key)
time.sleep(hold)
self.release(key)
time.sleep(interval)

except (ValueError, self.InvalidKeyException):
raise self.InvalidCharacterException(i, character)
Expand Down
2 changes: 1 addition & 1 deletion tests/keyboard_controller_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down