-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetch.py
More file actions
executable file
·56 lines (51 loc) · 1.47 KB
/
getch.py
File metadata and controls
executable file
·56 lines (51 loc) · 1.47 KB
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
# class _GetchUnix:
# def __init__(self):
# import tty, sys
# from select import select
# def __call__(self):
# import sys, tty, termios
# from select import select
# fd = sys.stdin.fileno()
# old_settings = termios.tcgetattr(fd)
# try:
# tty.setraw(sys.stdin.fileno())
# [i, o, e] = select([sys.stdin.fileno()], [], [], 0.05)
# if i:
# ch=sys.stdin.read(1)
# else:
# ch=''
# finally:
# termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
# return ch
class _GetchUnix:
def __init__(self):
import tty, 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
getchS = _GetchUnix()
class AlarmException(Exception):
pass
def alarmHandler(signum, frame):
raise AlarmException
def getch(timeout=0.05):
import signal
signal.signal(signal.SIGALRM, alarmHandler)
signal.setitimer(signal.ITIMER_REAL,timeout,timeout)
try:
text = getchS()
signal.alarm(0)
return text
except AlarmException:
pass
signal.signal(signal.SIGALRM, signal.SIG_IGN)
return ''