-
Notifications
You must be signed in to change notification settings - Fork 22
/
nbinput.py
173 lines (135 loc) · 4.26 KB
/
nbinput.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
Most of this code came from this page:
http://code.activestate.com/recipes/134892/#c5
"""
import sys
import select
UP, DOWN, RIGHT, LEFT = 'A', 'B', 'C', 'D'
class NonBlockingInput:
"""
Gets a single character from standard input. Does not echo to the
screen.
"""
def __init__(self):
try:
self.impl = _nbiGetchWindows()
except ImportError:
try:
self.impl = _nbiGetchMacCarbon()
except (AttributeError, ImportError):
self.impl = _nbiGetchUnix()
def char(self):
try:
return self.impl.char().replace('\r\n', '\n').replace('\r', '\n')
except:
return None
def __enter__(self):
self.impl.enter()
return self
def __exit__(self, type_, value, traceback):
self.impl.exit(type_, value, traceback)
def escape_code(self):
first, char = self.char(), self.char()
if first == '[' and self.char() == chr(27):
return char
return first
class _nbiGetchUnix:
def __init__(self):
# Import termios now or else you'll get the Unix version on the Mac.
import tty
import termios
self.tty = tty
self.termios = termios
def enter(self):
self.old_settings = self.termios.tcgetattr(sys.stdin)
self.tty.setcbreak(sys.stdin.fileno())
def exit(self, type_, value, traceback):
self.termios.tcsetattr(sys.stdin,
self.termios.TCSADRAIN, self.old_settings)
def char(self):
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
return sys.stdin.read(1)
return None
class _nbiGetchWindows:
def __init__(self):
import msvcrt
self.msvcrt = msvcrt
def enter(self):
pass
def exit(self, type_, value, traceback):
pass
def char(self):
if self.msvcrt.kbhit():
try:
return str(self.msvcrt.getch(), encoding='UTF-8')
except:
pass
return None
class _nbiGetchMacCarbon:
"""
A function which returns the current ASCII key that is down;
if no ASCII key is down, the null string is returned. The
page http://www.mactech.com/macintosh-c/chap02-1.html was
very helpful in figuring out how to do this.
"""
def __init__(self):
# See if teminal has this (in Unix, it doesn't)
import Carbon
self.Carbon = Carbon
self.Carbon.Evt
def enter(self):
pass
def exit(self, type_, value, traceback):
pass
def char(self):
if self.Carbon.Evt.EventAvail(0x0008)[0] == 0: # 0x0008 is the keyDownMask
return ''
else:
# The event contains the following info:
# (what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]
#
# The message (msg) contains the ASCII char which is
# extracted with the 0x000000FF charCodeMask; this
# number is converted to an ASCII character with chr() and
# returned.
_, msg, _, _, _ = self.Carbon.Evt.GetNextEvent(0x0008)[1]
return chr(msg & 0x000000FF)
class BlockingInput(NonBlockingInput):
"""
Gets a single character from standard input. Does not echo to the
screen.
"""
def __init__(self):
try:
self.impl = _biGetchWindows()
except ImportError:
try:
self.impl = _biGetchMacCarbon()
except (AttributeError, ImportError):
self.impl = _biGetchUnix()
def escape_code(self):
first = self.char()
if first == chr(27) and self.char() == '[':
return self.char()
return first
class _biGetchUnix(_nbiGetchUnix):
def char(self):
return sys.stdin.read(1)
class _biGetchWindows(_nbiGetchWindows):
def char(self):
try:
return str(self.msvcrt.getch(), encoding='UTF-8')
except:
return None
class _biGetchMacCarbon(_nbiGetchMacCarbon):
pass
def main():
import time
with BlockingInput() as bi:
while True:
c = bi.escape_code()
if c == chr(27):
break
print(c)
if __name__ == '__main__':
main()