-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_keypress.py
77 lines (60 loc) · 2.39 KB
/
python_keypress.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
import serial, sys
from time import sleep
from pynput.keyboard import Key, Controller
keyboard = Controller()
def press(char):
keyboard.press(char)
sleep(0.025)
keyboard.release(char)
#sleep(0.1)
COM = 'COM7'# /dev/ttyACM0 (Linux)
BAUD = 9600
ser = serial.Serial(COM, BAUD, timeout = .1)
print('Waiting for device');
sleep(3)
print(ser.name)
#check args
if("-m" in sys.argv or "--monitor" in sys.argv):
monitor = True
else:
monitor= False
btn_prev = None
keys_press = [None, None]
while True:
try:
#print(keys_press)
val = str(ser.readline().decode().strip('\r\n'))#Capture serial output as a decoded string
if "X Axis" in val:
if float(val.strip("X Axis")) < 2.5 and float(val.strip("X Axis")) >= 0:
if keys_press[0] == Key.right:
keyboard.release(Key.right)
keyboard.press(Key.left)
keys_press[0] = Key.left
elif float(val.strip("X Axis")) > 2.5:
if keys_press[0] == Key.left:
keyboard.release(Key.left)
keyboard.press(Key.right)
keys_press[0] = Key.right
elif (val.strip("X Axis")) == "-1":
if keys_press[0] != None:
keyboard.release(keys_press[0])
elif "Y Axis" in val:
if float(val.strip("Y Axis")) < 2.5 and float(val.strip("Y Axis")) >= 0:
if keys_press[1] == Key.down:
keyboard.release(Key.down)
keyboard.press(Key.up)
keys_press[1] = Key.up
elif float(val.strip("Y Axis")) > 2.5:
if keys_press[1] == Key.up:
keyboard.release(Key.up)
keyboard.press(Key.down)
keys_press[1] = Key.down
elif (val.strip("Y Axis")) == "-1":
if keys_press[1] != None:
keyboard.release(keys_press[1])
elif val == "High1" and val != btn_prev: #Press door only once
press(Key.space)
elif val == "High2": #IE, keep on shooting. Gun delay means that this wont be an issue.
press(Key.ctrl_l)
except:
print("Exception")