-
Notifications
You must be signed in to change notification settings - Fork 1
/
getch.py
56 lines (43 loc) · 1.3 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
""""
This module sets of the getch() function, which is like input(), but only
takes a single character, and doesn't require the user to press enter.
Credit:
https://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user
"""
# For python 3 compatibility
from __future__ import division, absolute_import, print_function
try: input = raw_input
except: pass
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): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, 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:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
def getch(message=None):
if message:
print(message)
getchfunc = _Getch()
return getchfunc()