forked from Chalkeys/Yolov8-Apex-Aim-assist-with-IFF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyListener.py
171 lines (149 loc) · 5.57 KB
/
MyListener.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from pynput import mouse, keyboard
import numpy as np
import pyautogui
import time
import win32api
import win32con
import win32gui
import win32print
import winsound
Start_detection = False
left_lock = False # 左键锁, Left, 按鼠标左键时锁
right_lock = False # 右键锁, Right, 按鼠标右键(开镜)时锁
old_status = False
Listen = True
width = 0
interval = 0.01
screen_size = np.array([win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1)])
screen_center = np.array(screen_size, dtype=int) // 2
destination = screen_center
last = destination
backforce = 0
hDC = win32gui.GetDC(0)
scale = win32print.GetDeviceCaps(hDC, win32con.LOGPIXELSX) / 96
def get_S_L():
global Start_detection
global Listen
return Start_detection, Listen
# if the esc is clicked, return True
def listen_key(key):
if key == keyboard.Key.home:
global Start_detection, Listen
Listen = False
Start_detection = False
print("Stop listening")
return False
if key == keyboard.Key.end:
Start_detection = not Start_detection
print("Start detection: ", Start_detection)
if key == keyboard.Key.shift:
Start_detection = True
# print("Start detection: ", Start_detection)
if key == keyboard.Key.left:
global left_lock
left_lock = not left_lock
winsound.Beep(800 if left_lock else 400, 200)
if key == keyboard.Key.right:
global right_lock
right_lock = not right_lock
winsound.Beep(800 if right_lock else 400, 200)
# TEMPORARY - detect if key shift is released
def keys_released(key):
if key == keyboard.Key.shift:
global Start_detection
Start_detection = False
# print("Start detection: ", Start_detection)
# if the right mouse is clicked, return True
def listen_mouse(x, y, button, pressed):
global Start_detection, old_status, backforce, left_lock, right_lock
if button == mouse.Button.right and right_lock:
if pressed:
Start_detection = True
print("Start detection: ", Start_detection)
if not pressed:
Start_detection = False
print("Start detection: ", Start_detection)
if button == mouse.Button.left and left_lock:
if pressed:
Start_detection = True
backforce = 3
print("Start detection: ", Start_detection)
if not pressed:
Start_detection = False
backforce = 0
print("Start detection: ", Start_detection)
def speed_func(x, speed, smooth):
global width
return max(x / smooth / speed, width / 3)
pre_error = intergral = np.array([0., 0.])
def PID(args, error):
global pre_error, intergral, backforce
intergral += error
derivative = error - pre_error
pre_error = error
output = args.Kp * error + args.Ki * intergral + args.Kd * derivative
output[1] += backforce
pre_error = error
return output.astype(int)
def Move_Mouse(args):
global screen_size, screen_center, last
# while Listen:
global destination, width, interval, pre_error, intergral
if Start_detection:
pos = np.array(win32api.GetCursorPos(), dtype=int)
mouse_vector = (destination - pos) / scale
norm = np.linalg.norm(mouse_vector)
if args.pid:
if (destination[0] == -1 and destination[1] == -1):
if last[0] == -1:
pre_error = intergral = np.array([0., 0.])
mouse_vector = np.array([0, 0])
return
else:
mouse_vector = np.array([0, 0])
move = PID(args, mouse_vector)
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, int(move[0]), int(move[1]))
return
# if destination not in region
if norm <= 2 or (destination[0] == screen_center[0] and destination[1] == screen_center[1]): return
if norm <= width * 2 / 3:
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, int(mouse_vector[0] / 2), int(mouse_vector[1] / 2))
return
des = mouse_vector / args.smooth
for i in range(int(args.smooth)):
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, int(des[0]), int(des[1]))
time.sleep(0.01 / args.smooth)
delay_time = 2.4 / float(args.game_fps)
time.sleep(delay_time)
else:
pre_error = intergral = np.array([0., 0.])
# redirect the mouse closer to the nearest box center
def Mouse_redirection(boxes, args, tpf):
global destination, width, interval, screen_size, screen_center, last
if boxes.shape[0] == 0:
last = destination
destination = np.array([-1, -1])
return
interval = tpf
pos = np.array(win32api.GetCursorPos(), dtype=int)
# Get the center of the boxes
boxes_center = (
(boxes[:, :2] + boxes[:, 2:]) / 2
)
boxes_center[:, 1] = (
boxes[:, 1] * 0.6 + boxes[:, 3] * 0.4
)
# Map the box from the image coordinate to the screen coordinate
screen_center = screen_size / 2
start_point = screen_center - screen_size[1] * args.crop_size / 2
start_point = list(map(int, start_point))
boxes_center[:, 0] = boxes_center[:, 0] + start_point[0]
boxes_center[:, 1] = boxes_center[:, 1] + start_point[1]
# Find the nearest box center
dis = np.linalg.norm(boxes_center - pos, axis=-1)
min_index = np.argmin(dis)
width = boxes[min_index, 2] - boxes[min_index, 0]
last = destination
destination = boxes_center[np.argmin(dis)].astype(int)
# print(destination)
# mouse_instance.position = tuple(boxes_center[np.argmin(dis)])