-
Notifications
You must be signed in to change notification settings - Fork 0
/
slow-down.py
executable file
·43 lines (34 loc) · 1 KB
/
slow-down.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
import argparse
import pynput
import time
DEFAULT_WPM = 40
BUFFER_S = 1 # seconds
####### PARSE
parser = argparse.ArgumentParser(description='Yell at you when you type too fast.')
parser.add_argument('--wpm', action='store', type=int, default=DEFAULT_WPM,
help='Maximum typing speed in wpm')
args = parser.parse_args()
MAX_STROKES_PER_SECOND = args.wpm * 5 / 60
MAX_TYPING_SLICE = 1. / MAX_STROKES_PER_SECOND
####### RATE LIMITER
global nextAllowedKeyStroke
nextAllowedKeyStroke = time.time()
def rateLimit(k):
global nextAllowedKeyStroke
t = time.time()
#print(t, nextAllowedKeyStroke)
if nextAllowedKeyStroke > t + BUFFER_S:
nextAllowedKeyStroke = t + BUFFER_S
print("\a")
else:
nextAllowedKeyStroke = max(nextAllowedKeyStroke, time.time()) + MAX_TYPING_SLICE
def start():
keyStateListener = pynput.keyboard.Listener(
on_press=rateLimit
#on_release=rateLimit
)
keyStateListener.start()
while(True):
time.sleep(5) # long-living process
start()