-
Notifications
You must be signed in to change notification settings - Fork 0
/
inp.py
31 lines (26 loc) · 801 Bytes
/
inp.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
import atexit
import termios
import sys
import os
old_settings=None
def init_anykey():
global old_settings
old_settings = termios.tcgetattr(sys.stdin)
new_settings = termios.tcgetattr(sys.stdin)
new_settings[3] = new_settings[3] & ~(termios.ECHO | termios.ICANON) # lflags
new_settings[6][termios.VMIN] = 0 # cc
new_settings[6][termios.VTIME] = 0 # cc
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, new_settings)
@atexit.register
def term_anykey():
global old_settings
if old_settings:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
def anykey():
ch_set = []
ch = os.read(sys.stdin.fileno(), 1)
while ch != None and len(ch) > 0:
ch_set.append( chr(ch[0]) )
ch = os.read(sys.stdin.fileno(), 1)
return ch_set;
init_anykey()