-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetch.py
57 lines (43 loc) · 1.27 KB
/
getch.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
from __future__ import unicode_literals
class _Getch:
"""Gets a single character from standard input. Does not echo to the screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self):
char = self.impl()
if char == '\x03':
raise KeyboardInterrupt
elif char == '\x04':
raise EOFError
print(char)
return char
class _GetchUnix:
# noinspection PyUnresolvedReferences,PyUnresolvedReferences
def __init__(self):
import tty
import sys
def __call__(self):
import sys
import tty
import termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
# noinspection PyUnresolvedReferences
def __init__(self):
# noinspection PyUnresolvedReferences
import msvcrt
def __call__(self):
# noinspection PyUnresolvedReferences
import msvcrt
return msvcrt.getch()
getch = _Getch()