forked from fabius8/chromeAuto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynClickV2.py
311 lines (258 loc) · 10.4 KB
/
synClickV2.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import win32gui
import win32con
import win32api
import psutil
import win32process
import re
import time
import json
import win32clipboard
#import pyautogui
config = json.load(open('config.json'))
k = config["k"]
def sort_by_port(cmdline):
port_str = next((param for param in cmdline if param.startswith('--remote-debugging-port=')), None)
if port_str:
port = int(re.search(r'\d+', port_str).group())
return port
else:
return 0
def enum_chrome_windows(hwnd, _):
classname = win32gui.GetClassName(hwnd)
title = win32gui.GetWindowText(hwnd)
if classname == 'Chrome_WidgetWin_1' and title and win32gui.GetParent(hwnd) == 0: # 必须是父窗口
print(title)
chrome_windows.append(hwnd)
def is_window_valid(hwnd):
return win32gui.IsWindow(hwnd)
def filter_invalid_windows(chrome_windows):
return [hwnd for hwnd in chrome_windows if is_window_valid(hwnd)]
# def click(x, y):
# print(x, y)
# win32api.SetCursorPos((x, y))
# win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
# win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
chrome_windows = []
win32gui.EnumWindows(enum_chrome_windows, None)
my_chrome_windows = []
ports = []
for hwnd in chrome_windows:
print(hwnd)
process = psutil.Process(win32process.GetWindowThreadProcessId(hwnd)[1])
if any("--remote-debugging-port" in arg for arg in process.cmdline()):
print(process.cmdline())
port_str = next((param for param in process.cmdline() if param.startswith('--remote-debugging-port=')), None)
if port_str:
port = int(re.search(r'\d+', port_str).group())
ports.append(port)
my_chrome_windows.append(hwnd)
else:
continue
print(ports, my_chrome_windows)
sorted_chrome_windows = [x for _, x in sorted(zip(ports, my_chrome_windows))]
print(sorted_chrome_windows)
from pynput import mouse, keyboard
from pynput.keyboard import Key, Controller
kb = Controller()
# 定义变量来保存上一次点击的位置
last_click_position = (0, 0)
# 在程序开始时初始化一个变量来表示程序的暂停状态
paused = False
for hwnd in sorted_chrome_windows:
# 将焦点设置到Chrome窗口
#print(hwnd)
win32gui.SetForegroundWindow(hwnd)
# 获取窗口的位置和大小
rect = win32gui.GetWindowRect(hwnd)
x = rect[0]
y = rect[1]
w = rect[2] - x
h = rect[3] - y
#print(rect)
kb.press(Key.alt)
kb.release(Key.alt)
#click(x + w // 2, y + h // 2)
#time.sleep(1)
#wParam = win32api.MAKELONG(0, 120)
#lParam = win32api.MAKELONG(x + w // 2, y + h // 2) # 计算鼠标位置参数
sorted_chrome_windows = filter_invalid_windows(sorted_chrome_windows)
print("filter:", sorted_chrome_windows)
""" for handle in sorted_chrome_windows:
print("handle", handle)
print("follow scroll", -1 * 120)
win32gui.SetForegroundWindow(handle)
win32api.SendMessage(handle, win32con.WM_MOUSEWHEEL, 120, 0) """
# 定义鼠标点击监听函数
def on_click(x, y, button, pressed):
global last_click_position
if paused:
print("paused!")
return
if pressed:
last_click_position = (x, y)
print(f"Clicked at {last_click_position}")
if button == mouse.Button.left and not pressed: # 监听鼠标左键按下事件
print('鼠标左键按下,位置:', x, y)
rect = win32gui.GetWindowRect(sorted_chrome_windows[0])
rx = rect[0]
ry = rect[1]
rw = rect[2] - rx
rh = rect[3] - ry
# 对位置和大小进行150%缩放
x_scaled = int(x / k)
y_scaled = int(y / k)
print(rx, ry, rw, rh, x_scaled, y_scaled)
if (x_scaled < rx) or (x_scaled > (rx + rw)) or (y_scaled < ry) or (y_scaled > (ry + rh)):
print("not win#1, skip")
return
# 相对窗口位置
x_related = x_scaled - rx
y_related = y_scaled - ry
# 在其他9个窗口相对位置执行鼠标按键操作
for handle in sorted_chrome_windows:
#print("handle", handle)
if handle == sorted_chrome_windows[0]:
continue
#win32gui.SetForegroundWindow(handle)
lParam = win32api.MAKELONG(x_related, y_related) # 计算鼠标位置参数
#win32gui.SendMessage(handle, win32con.WM_ACTIVATE, win32con.WA_ACTIVE, 0)
win32api.SendMessage(handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
win32api.SendMessage(handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, lParam)
#time.sleep(1)
#win32api.SetCursorPos((x_related, y_related))
# 定义键盘监听函数
def on_press(key):
global paused
if isinstance(key, keyboard.KeyCode):
if paused:
print("paused!")
return
rect = win32gui.GetWindowRect(sorted_chrome_windows[0])
rx = rect[0]
ry = rect[1]
rw = rect[2] - rx
rh = rect[3] - ry
# 对位置和大小进行150%缩放
x_scaled = int(last_click_position[0] / k)
y_scaled = int(last_click_position[1] / k)
print(rx, ry, rw, rh, x_scaled, y_scaled)
if (x_scaled < rx) or (x_scaled > (rx + rw)) or (y_scaled < ry) or (y_scaled > (ry + rh)):
print("not win#1, skip")
return
# 普通按键
print('普通按键:', key.char)
vk_code = ord(key.char) # 获取按键对应的虚拟键码
print('键码', vk_code)
for handle in sorted_chrome_windows:
#print("handle", handle)
if handle == sorted_chrome_windows[0]:
continue
win32api.SendMessage(handle, win32con.WM_CHAR, vk_code, 0) # 发送按键按下消息
#win32api.SendMessage(handle, win32con.WM_KEYUP, vk_code, 0) # 发送按键释放消息
#
if str(key) == r"'\x16'": # ctrl + v
print("ctrl+v paste press")
for handle in sorted_chrome_windows:
#print("handle", handle)
if handle == sorted_chrome_windows[0]:
continue
#win32gui.SetForegroundWindow(handle)
# win32clipboard.OpenClipboard()
# data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
# print(data)
# win32clipboard.CloseClipboard()
# win32gui.SendMessage(handle, win32con.WM_PASTE, 0, "data")
win32api.SendMessage(handle, win32con.WM_KEYDOWN, win32con.VK_CONTROL, 0) # 发送按键按下消息
win32api.SendMessage(handle, win32con.WM_KEYDOWN, 0x56, 0)
win32api.SendMessage(handle, win32con.WM_KEYUP, 0x56, 0)
win32api.SendMessage(handle, win32con.WM_KEYUP, win32con.VK_CONTROL, 0) # 发送按键按下消息
#time.sleep(0.5)
elif key in [Key.enter, Key.shift, Key.ctrl_l, Key.alt_l, Key.f9, Key.f4, Key.backspace, Key.esc]:
# 特殊按键
print('special key {0} pressed'.format(key))
if key == Key.f9: # 监听F9键按下事件
# 退出程序
print("exit...")
mouse_listener.stop()
keyboard_listener.stop()
return False
# 切换暂停状态
if key == Key.f4: # 监听f4键按下事件
paused = not paused
if paused:
print("paused...")
return
else:
print("resumed...")
return
if key == Key.backspace or key == Key.enter:
rect = win32gui.GetWindowRect(sorted_chrome_windows[0])
rx = rect[0]
ry = rect[1]
rw = rect[2] - rx
rh = rect[3] - ry
# 对位置和大小进行150%缩放
x_scaled = int(last_click_position[0] / k)
y_scaled = int(last_click_position[1] / k)
print(rx, ry, rw, rh, x_scaled, y_scaled)
if (x_scaled < rx) or (x_scaled > (rx + rw)) or (y_scaled < ry) or (y_scaled > (ry + rh)):
print("not win#1, skip")
return
for handle in sorted_chrome_windows:
#print("handle", handle)
if handle == sorted_chrome_windows[0]:
continue
if key == Key.enter:
win32api.SendMessage(handle, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) # 发送按键按下消息
win32api.SendMessage(handle, win32con.WM_KEYUP, win32con.VK_RETURN, 0) # 发送按键释放消息
if key == Key.backspace:
win32api.SendMessage(handle, win32con.WM_KEYDOWN, win32con.VK_BACK, 0) # 发送按键按下消息
win32api.SendMessage(handle, win32con.WM_KEYUP, win32con.VK_BACK, 0) # 发送按键释放消息
else:
# 其他按键
print('其他按键')
def on_scroll(x, y, dx, dy):
if paused:
print("paused!")
return
print('Scrolled {0} at {1}'.format(
'down' if dy < 0 else 'up',
(x, y, dx, dy)))
rect = win32gui.GetWindowRect(sorted_chrome_windows[0])
rx = rect[0]
ry = rect[1]
rw = rect[2] - rx
rh = rect[3] - ry
# 对位置和大小进行150%缩放
x_scaled = int(x / k)
y_scaled = int(y / k)
print(rx, ry, rw, rh, x_scaled, y_scaled)
if (x_scaled < rx) or (x_scaled > (rx + rw)) or (y_scaled < ry) or (y_scaled > (ry + rh)):
print("not win#1, skip")
return
# 相对窗口位置
x_related = x_scaled - rx
y_related = y_scaled - ry
for handle in sorted_chrome_windows:
print("handle", handle)
if handle == sorted_chrome_windows[0]:
continue
rect = win32gui.GetWindowRect(handle)
x = rect[0]
y = rect[1]
w = rect[2] - x
h = rect[3] - y
#print("follow scroll", dy * 120)
#win32gui.SetForegroundWindow(handle)
wParam = win32api.MAKELONG(0, dy*120)
lParam = win32api.MAKELONG(x + x_related, y + y_related)
win32api.SendMessage(handle, win32con.WM_MOUSEWHEEL, wParam, lParam)
# 创建鼠标监听器
mouse_listener = mouse.Listener(on_click=on_click, on_scroll=on_scroll)
mouse_listener.start()
# 创建键盘监听器
keyboard_listener = keyboard.Listener(on_press=on_press)
keyboard_listener.start()
# 进入消息循环
mouse_listener.join()
keyboard_listener.join()