-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect-keyboard.py
45 lines (33 loc) · 966 Bytes
/
detect-keyboard.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
#!/usr/bin/python3
# adapted from https://github.com/recantha/EduKit3-RC-Keyboard/blob/master/rc_keyboard.py
import sys, termios, tty, os, time
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
button_delay = 0.2
while True:
char = getch()
if (char == "p"):
print("Stop!")
exit(0)
if (char == "a"):
print("Left pressed")
time.sleep(button_delay)
elif (char == "d"):
print("Right pressed")
time.sleep(button_delay)
elif (char == "w"):
print("Up pressed")
time.sleep(button_delay)
elif (char == "s"):
print("Down pressed")
time.sleep(button_delay)
elif (char == "1"):
print("Number 1 pressed")
time.sleep(button_delay)