-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathkeyos.py
More file actions
32 lines (30 loc) · 899 Bytes
/
keyos.py
File metadata and controls
32 lines (30 loc) · 899 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
# -*- coding: utf-8 -*-
import termios, sys, os
#import termios, TERMIOS, sys, os
TERMIOS = termios
def getkey():
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
new[6][TERMIOS.VMIN] = 1
new[6][TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
c = None
try:
c = os.read(fd, 1)
finally:
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
return c
if __name__ == '__main__':
print 'type something'
s = ''
while 1:
c = getkey()
if c == '\n': ## break on a Return/Enter keypress
break
if c== 'q':
print "quit"
print 'got', c
s = s + c
print s