forked from decentropy/SentryTurret
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathKeyboardPoller.py
42 lines (38 loc) · 1.05 KB
/
KeyboardPoller.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
#!/usr/bin/python
import fcntl
import os
import sys
import termios
import threading
#globals
keypressed = threading.Event()
key = ""
def getch():
fd = sys.stdin.fileno()
oldattr = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
try:
while True:
try:
c = sys.stdin.read(1)
except IOError:
pass
else:
return c
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldattr)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
class WaitKey() :
def __init__(self):
global key, keypressed
keypressed.clear()
key = ""
self.thread = threading.Thread(target=self.run)
def run(self) :
global key, keypressed
key = getch()
keypressed.set()