-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkbhit.py
42 lines (33 loc) · 1010 Bytes
/
kbhit.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
import atexit
import os
import sys
import termios
from select import select
class KBHit:
def __init__(self):
self._fd = sys.stdin.fileno()
self._prev_term = termios.tcgetattr(self._fd)
self._next_term = termios.tcgetattr(self._fd)
self._next_term[3] = self._next_term[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(self._fd, termios.TCSAFLUSH, self._next_term)
atexit.register(self.set_normal_term)
def set_normal_term(self):
"""
reset to normal terminal
"""
termios.tcsetattr(self._fd, termios.TCSAFLUSH, self._prev_term)
def getch(self):
""" """
return sys.stdin.read(1)
def kbhit(self):
"""
Determine whether character was hit
on keyboard
"""
dr, dw, de = select([sys.stdin], [], [], 0)
return dr != []
def flush(self):
"""
clear input buffer
"""
termios.tcflush(sys.stdin, termios.TCIOFLUSH)