forked from spandanb/vis-imp-nav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getch2.py
70 lines (58 loc) · 1.71 KB
/
getch2.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import sys, tty, termios
import select
class _Getch: #Only works on *ix
def __init__(self):
pass
def __call__(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
if ord(ch) == 27: sys.exit(0)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def fetch():
i,o,e = select.select([sys.stdin],[],[],0.01)
for s in i:
if s == sys.stdin:
input = sys.stdin.readline()
return input
return False
class Getch(): #Non-blocking
def __init__(self):
pass
def __call__(self):
#timeout in seconds; smaller number -> less blocking
timeout = 0.01 #1/100 of seconds
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
ch = False
try:
tty.setraw(sys.stdin.fileno())
i,o,e = select.select([sys.stdin],[],[],timeout)
for s in i:
if s == sys.stdin:
ch = sys.stdin.read(1)
if ord(ch) == 27: sys.exit(0)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
if __name__ == '__main__':
"""
getch = Getch()
while True:
ch = getch()
if ch:
if ord(ch) == 27:
sys.exit()
else:
print "User entered {}".format(ch)
else:
pass #print "blank"
"""
while True:
resp = fetch()
if resp:
print "User entered {}".format(resp[0])