-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetinput.py
More file actions
82 lines (65 loc) · 1.63 KB
/
Copy pathgetinput.py
File metadata and controls
82 lines (65 loc) · 1.63 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
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
import sys
import select
import tty
import termios
import subprocess as sp
def clear():
'''
function to clear the terminal screen
'''
sp.call('clear', shell=True)
def keypress(c):
# q/Q
if c == '\x51' or c == '\x71':
return -1
# d/D
elif c == '\x44' or c == '\x64':
return 1
# a/A
elif c == '\x41' or c == '\x61':
return 2
# w/W
elif c == '\x57' or c == '\x77':
return 3
# Spacebar
elif c == '\x20':
return 5
#s/S
elif c == '\x53' or c == '\x73':
return 4
class NBInput:
'''
class to deal with non-blocking input
'''
def __init__(self):
'''
Initializes the object to be used for non-blocking input.
- Saves original state at time of function call
- Conversion to new mode has to be manual
'''
self.old_settings = termios.tcgetattr(sys.stdin)
def nbTerm(self):
'''
Sets up the terminal for non-blocking input
'''
tty.setcbreak(sys.stdin.fileno())
def orTerm(self):
'''
Sets terminal back to original state
'''
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, self.old_settings)
def kbHit(self):
'''
returns True if keypress has occured
'''
return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])
def getCh(self):
'''
returns input character
'''
return sys.stdin.read(1)
def flush(self):
'''
clears input buffer
'''
termios.tcflush(sys.stdin, termios.TCIOFLUSH)