-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_input.py
34 lines (26 loc) · 921 Bytes
/
key_input.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
import termios
import sys
import atexit
from select import select
class KeyInput:
def __init__(self):
self.__fd = sys.stdin.fileno()
self.__new_term = termios.tcgetattr(self.__fd)
self.__old_term = termios.tcgetattr(self.__fd)
# New terminal setting unbuffered
self.__new_term[3] = (self.__new_term[3] & ~
termios.ICANON & ~termios.ECHO)
termios.tcsetattr(self.__fd, termios.TCSAFLUSH, self.__new_term)
# Support normal-terminal reset at exit
atexit.register(self.reset)
def reset(self):
termios.tcsetattr(self.__fd, termios.TCSAFLUSH, self.__old_term)
@staticmethod
def get():
return sys.stdin.read(1)
@staticmethod
def input_given():
return select([sys.stdin], [], [], 0)[0] != []
@staticmethod
def clear():
termios.tcflush(sys.stdin, termios.TCIFLUSH)