Skip to content

Add key press duration and time between key presses in Controller().type() #606

@XaverKlemenschits

Description

@XaverKlemenschits

Description
The Controller().type() function is very useful to easily type out long strings.
However, some web interfaces are too slow to capture the key presses because the key is released directly after being pressed [1]. I will be using duration to refer to the time a key is pressed and delay to refer to the time between two consecutive keys being pressed.

There is already a pull request to add a delay between key presses in #579 , but it does not include duration. As mentioned in [2], the xdotool utility provides the --delay option to set how long the key press should last [3][4], but the time is apparently split 50-50 between duration and delay.

I would propose to add both parameters to Controller().type() as described in [2], but keep the current default of no duration or delay by default.

    def type(self, string, duration=0.0, delay=0.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 duration float: Key press duration in seconds.
        :param delay float: Time between two key presses in seconds.

        :raises InvalidCharacterException: if an untypable character is
            encountered
        """
        from . import _CONTROL_CODES
        from time import sleep
        for i, character in enumerate(string):
            key = _CONTROL_CODES.get(character, character)
            try:
                self.press(key)
                if duration > 0.0: # prevent context switch if duration is 0
                    sleep(duration)
                self.release(key)
                if delay > 0.0:
                     sleep(delay)

            except (ValueError, self.InvalidKeyException):
                raise self.InvalidCharacterException(i, character)

[1] https://github.com/moses-palmer/pynput/blob/6d01a92d2e4ef10be57af6ef7ce720f04f8b5652/lib/pynput/keyboard/_base.py#L488C1-L492C34
[2] Evidlo/passhole#64
[3] https://askubuntu.com/questions/1098762/does-xdotool-key-delay-pause-for-specified-delay-between-typing-every-two-co
[4] jordansissel/xdotool#423

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions