forked from ppaez/alsaseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kbhit.py
102 lines (86 loc) · 2.46 KB
/
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
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
"""
kbhit.py - kbhit functionality for Unix (with termios)
How to use this module:
unbuffer_stdin()
Call unbuffer_stdin() before using any other functions.
kbhit()
Once stdin is in unbuffered mode, you can call kbhit() to
see if a key is ready on stdin. kbhit() will return immediately.
Returns 1 if a key is ready, 0 otherwise.
getchar()
Calling getchar() will return one keypress from stdin. If a
key is not ready, then getchar() will block until the next keypress.
Recommended usage is to call kbhit() until a key is ready,
then getchar() to get the keypress.
restore_stdin()
When you are all done, call restore_stdin() to return stdin
to its prior state.
This module was tested with Linux.
This module is NOT thread-safe.
Author: Tim Bird, (termios handling from Andrew Kuchling)
"""
import sys
import os
import termios
#import TERMIOS
import select
# prepare stdin for kbhit usage
def unbuffer_stdin():
"""unbuffer_stdin() - used to convert stdin to
unbuffered mode. That is, you can read a single character
from stdin, without having to wait for a whole line of input
"""
global old_termios
fd = sys.stdin.fileno()
old_termios = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
# turn off canonical mode and echo
new[3]=new[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, new)
# stop using stdin for kbhit
def restore_stdin():
"""restore_stdin() - used to convert stdin to
whatever mode it was in before calling unbuffer_stdin()
"""
global old_termios
termios.tcsetattr(sys.stdin.fileno(),
termios.TCSAFLUSH, old_termios)
# returns 0 if no key ready, or 1 if a key is ready
def kbhit():
"""kbhit() - returns 1 if a key is ready, 0 othewise.
kbhit always returns immediately.
"""
(read_ready, write_ready, except_ready) = \
select.select( [sys.stdin], [], [], 0.0)
if read_ready:
return 1
else:
return 0
# get the last key hit
def getch():
"""getchar() - reads one key from stdin. Waits if there
is not a key available.
"""
return os.read(sys.stdin.fileno(), 1)
def test():
unbuffer_stdin()
count = 0
print("A trail of '*'s will be written while I poll for keystrokes")
print("Press 'q' to quit")
while 1:
ready = kbhit()
if ready:
count = 0
key = getchar()
os.write(sys.stdout.fileno(), key)
if key=='q':
print()
break
else:
count = count + 1
if count==1000:
os.write(sys.stdout.fileno(), '*'.encode())
count = 0
restore_stdin()
if __name__=='__main__':
test()