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

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

Open
XaverKlemenschits opened this issue Jul 15, 2024 · 1 comment

Comments

@XaverKlemenschits
Copy link

XaverKlemenschits commented Jul 15, 2024

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

@nat3058
Copy link

nat3058 commented Nov 15, 2024

this is really good and helpful for me. I think this should be merged. Thanks for adding @XaverKlemenschits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants