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

Added new feature. Random delay in wite() function #513

Open
wants to merge 4 commits into
base: master
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: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ Like [`restore_state`](#keyboard.restore_state), but only restores modifier keys

<a name="keyboard.write"/>

## keyboard.**write**(text, delay=0, restore\_state\_after=True, exact=None)
## keyboard.**write**(text, delay=0, restore\_state\_after=True, exact=None, delaymin=0,delaymax=0)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L926)

Expand All @@ -731,6 +731,8 @@ the text is typed, and modifiers are restored afterwards.

- `delay` is the number of seconds to wait between keypresses, defaults to
no delay.
- `delaymin` and `delaymax` are used when you want a dynamic delay. When delay
variable given, `delaymin` and `delaymax` will be ignored.
- `restore_state_after` can be used to restore the state of pressed keys
after the text is typed, i.e. presses the keys that were released at the
beginning. Defaults to True.
Expand Down
16 changes: 13 additions & 3 deletions keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def on_space():
import collections as _collections
from threading import Thread as _Thread, Lock as _Lock
import time as _time
import random
# Python2... Buggy on time changes and leap seconds, but no other good option (https://stackoverflow.com/questions/1205722/how-do-i-get-monotonic-time-durations-in-python).
_time.monotonic = getattr(_time, 'monotonic', None) or _time.time

Expand Down Expand Up @@ -924,8 +925,8 @@ def restore_modifiers(scan_codes):
Like `restore_state`, but only restores modifier keys.
"""
restore_state((scan_code for scan_code in scan_codes if is_modifier(scan_code)))

def write(text, delay=0, restore_state_after=True, exact=None):
def write(text, delay=0, restore_state_after=True, exact=None, delaymin=0,delaymax=0):
"""
Sends artificial keyboard events to the OS, simulating the typing of a given
text. Characters not available on the keyboard are typed as explicit unicode
Expand All @@ -936,6 +937,8 @@ def write(text, delay=0, restore_state_after=True, exact=None):

- `delay` is the number of seconds to wait between keypresses, defaults to
no delay.
- `delaymin` and `delaymax` are used when you want a dynamic delay. When delay
variable given, `delaymin` and `delaymax` will be ignored.
- `restore_state_after` can be used to restore the state of pressed keys
after the text is typed, i.e. presses the keys that were released at the
beginning. Defaults to True.
Expand All @@ -955,7 +958,11 @@ def write(text, delay=0, restore_state_after=True, exact=None):
send(letter)
else:
_os_keyboard.type_unicode(letter)
if delay: _time.sleep(delay)
if delay :
_time.sleep(delay)
elif delaymin and delaymax:
random_delay = random.uniform(delaymin,delaymax)
_time.sleep(random_delay)
else:
for letter in text:
try:
Expand All @@ -976,6 +983,9 @@ def write(text, delay=0, restore_state_after=True, exact=None):

if delay:
_time.sleep(delay)
elif delaymin and delaymax:
random_delay = random.uniform(delaymin,delaymax)
_time.sleep(random_delay)

if restore_state_after:
restore_modifiers(state)
Expand Down