-
Notifications
You must be signed in to change notification settings - Fork 115
/
keyreader.py
executable file
·106 lines (92 loc) · 4.01 KB
/
keyreader.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
import sys
import os
import termios, fcntl
import select
class KeyReader :
"""
Read keypresses one at a time, without waiting for a newline.
echo: should characters be echoed?
block: should we block for each character, or return immediately?
(If !block, we'll return None if nothing is available to read.)
"""
def __init__(self, echo=False, block=True):
"""Put the terminal into cbreak and noecho mode."""
self.fd = sys.stdin.fileno()
self.block = block
self.oldterm = termios.tcgetattr(self.fd)
self.oldflags = fcntl.fcntl(self.fd, fcntl.F_GETFL)
# Sad hack: when the destructor __del__ is called,
# the fcntl module may already be unloaded, so we can no longer
# call fcntl.fcntl() to set the terminal back to normal.
# So just in case, store a reference to the fcntl module,
# and also to termios (though I haven't yet seen a case
# where termios was gone -- for some reason it's just fnctl).
# The idea of keeping references to the modules comes from
# http://bugs.python.org/issue5099
# though I don't know if it'll solve the problem completely.
self.fcntl = fcntl
self.termios = termios
newattr = termios.tcgetattr(self.fd)
# tcgetattr returns: [iflag, oflag, cflag, lflag, ispeed, ospeed, cc]
# where cc is a list of the tty special characters (length-1 strings)
# except for cc[termios.VMIN] and cc[termios.VTIME] which are ints.
self.cc_save = newattr[6]
newattr[3] = newattr[3] & ~termios.ICANON
if not echo:
newattr[3] = newattr[3] & ~termios.ECHO
if block and False:
# VMIN and VTIME are supposed to let us do blocking reads:
# VMIN is the minimum number of characters before it will return,
# VTIME is how long it will wait if for characters < VMIN.
# This is documented in man termios.
# However, it doesn't work in python!
# In Python, read() never returns in non-canonical mode;
# even typing a newline doesn't help.
cc = self.cc_save[:] # Make a copy so we can restore VMIN, VTIME
cc[termios.VMIN] = 1
cc[termios.VTIME] = 0
newattr[6] = cc
else:
# Put stdin into non-blocking mode.
# We need to do this even if we're blocking, see above.
fcntl.fcntl(self.fd, fcntl.F_SETFL, self.oldflags | os.O_NONBLOCK)
termios.tcsetattr(self.fd, termios.TCSANOW, newattr)
def __del__(self):
"""Reset the terminal before exiting the program."""
self.termios.tcsetattr(self.fd, self.termios.TCSAFLUSH, self.oldterm)
self.fcntl.fcntl(self.fd, self.fcntl.F_SETFL, self.oldflags)
def getch(self):
"""Read keyboard input, returning a string.
Note that one key may result in a string of more than one character,
e.g. arrow keys that send escape sequences.
There may also be multiple keystrokes queued up since the last read.
This function, sadly, cannot read special characters like VolumeUp.
They don't show up in ordinary CLI reads -- you have to be in
a window system like X to get those special keycodes.
"""
# Since we can't use the normal cbreak read from python,
# use select to see if there's anything there:
if self.block:
inp, outp, err = select.select([sys.stdin], [], [])
try:
return sys.stdin.read()
except (IOError, TypeError) as e:
return None
def main():
import time
do_block = True
keyreader = KeyReader(echo=False, block=do_block)
while True:
key = keyreader.getch()
if key == 'q':
keyreader = None
return
if key:
print("-%s-" % key)
else:
print("None")
if not do_block:
time.sleep(.2)
if __name__ == '__main__':
main()