-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeypress.py
56 lines (45 loc) · 1.26 KB
/
keypress.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python3
import sys
import select
import time
def pollLine():
"""Return an typed line (after pressing enter) without blocking.
Returns None if enter has not been pressed."""
i, o, e = select.select([sys.stdin], [], [], 0.0001)
for s in i:
if s == sys.stdin:
input = sys.stdin.readline()
return input
return None
_prevChar = ""
_prevReturn = None
_prevTime = 0
def pollChar(sticky):
"""Returns the last char typed before pressing enter each time
enter has been pressed. Returns None if enter has not been pressed.
If `sticky` param is True, will return the same response for 0.1 secs."""
global _prevChar
global _prevReturn
global _prevTime
if sticky and (time.time() - _prevTime) < 0.1:
return _prevReturn
line = pollLine()
_prevTime = time.time()
if line:
line = line.strip()
if len(line) > 0:
_prevChar = line[-1:]
_prevReturn = _prevChar
return _prevChar
_prevReturn = None
return None
if __name__ == "__main__":
count = 0
while True:
line = pollChar(False)
if line:
print("READ: " + line)
else:
count += 1
print(count)
time.sleep(0.1)