-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetch.py
More file actions
34 lines (28 loc) · 833 Bytes
/
getch.py
File metadata and controls
34 lines (28 loc) · 833 Bytes
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
def _find_getch():
try:
import termios
except ImportError:
# Non-POSIX. Return msvcrt's (Windows') getch.
import msvcrt
return msvcrt.getch()
# POSIX system. Create and return a getch that manipulates the tty.
import sys, tty
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
return _getch
getch = _find_getch()
# It can do arrow keys but not ESC :-(
def getch_that_can_do_arrow_keys():
k = getch()
if ord(k) == 27:
k = getch()
if ord(k) == 91: # i think I should check for 224 in Windows
k = getch()
return k