-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkiro_auto_input.py
More file actions
260 lines (196 loc) · 7.08 KB
/
kiro_auto_input.py
File metadata and controls
260 lines (196 loc) · 7.08 KB
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
"""
自动化 Kiro 输入
1. 查找并激活 Kiro 窗口
2. 移动光标到指定位置
3. 点击并输入文本
4. 按回车发送
"""
import win32gui
import win32api
import win32con
import time
def find_and_activate_kiro():
"""查找并激活 Kiro 窗口"""
print("🔍 查找 Kiro 窗口...")
def find_window(hwnd, results):
if win32gui.IsWindowVisible(hwnd):
title = win32gui.GetWindowText(hwnd)
# 查找包含 "Kiro" 的窗口
if "Kiro" in title:
results.append((hwnd, title))
return True
results = []
win32gui.EnumWindows(find_window, results)
if not results:
print("❌ 未找到 Kiro 窗口")
print("💡 请确保 Kiro 已经打开")
return None, None
# 如果有多个 Kiro 窗口,显示列表
if len(results) > 1:
print(f"\n✅ 找到 {len(results)} 个 Kiro 窗口:")
for i, (hwnd, title) in enumerate(results):
print(f" [{i}] {title}")
# 使用第一个
hwnd, title = results[0]
print(f"\n使用第一个窗口: {title}")
else:
hwnd, title = results[0]
print(f"✅ 找到窗口: {title}")
print(f" 句柄: {hwnd}")
# 激活窗口
print("\n⚡ 激活窗口...")
try:
# 如果窗口最小化,先恢复
if win32gui.IsIconic(hwnd):
win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
time.sleep(0.3)
# 将窗口置于前台
win32gui.SetForegroundWindow(hwnd)
time.sleep(0.5)
print("✅ 窗口已激活")
return hwnd, title
except Exception as e:
print(f"⚠️ 激活窗口失败: {e}")
print(" 尝试使用 Alt+Tab 切换...")
# 尝试使用 Alt+Tab
win32api.keybd_event(win32con.VK_MENU, 0, 0, 0) # Alt down
win32api.keybd_event(win32con.VK_TAB, 0, 0, 0) # Tab down
time.sleep(0.05)
win32api.keybd_event(win32con.VK_TAB, 0, win32con.KEYEVENTF_KEYUP, 0) # Tab up
win32api.keybd_event(win32con.VK_MENU, 0, win32con.KEYEVENTF_KEYUP, 0) # Alt up
time.sleep(0.5)
return hwnd, title
def move_and_click(x, y):
"""移动鼠标到指定位置并点击"""
print(f"\n🖱️ 移动到位置: ({x}, {y})")
# 移动鼠标
win32api.SetCursorPos((x, y))
time.sleep(0.3)
print("✅ 鼠标已移动")
# 点击
print("🖱️ 点击...")
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
time.sleep(0.05)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
time.sleep(0.3)
print("✅ 已点击")
def input_text(text):
"""输入文本"""
print(f"\n⌨️ 输入文本: {text}")
try:
# 使用剪贴板粘贴
import win32clipboard
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(text, win32clipboard.CF_UNICODETEXT)
win32clipboard.CloseClipboard()
time.sleep(0.1)
# Ctrl+V 粘贴
win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
win32api.keybd_event(ord('V'), 0, 0, 0)
time.sleep(0.05)
win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
print("✅ 文本已粘贴")
except Exception as e:
print(f"⚠️ 剪贴板方法失败: {e}")
print(" 使用逐字符输入...")
# 逐字符输入
for char in text:
vk = win32api.VkKeyScan(char)
if vk != -1:
# 检查是否需要按 Shift
if vk & 0x100:
win32api.keybd_event(win32con.VK_SHIFT, 0, 0, 0)
win32api.keybd_event(vk & 0xFF, 0, 0, 0)
time.sleep(0.02)
win32api.keybd_event(vk & 0xFF, 0, win32con.KEYEVENTF_KEYUP, 0)
if vk & 0x100:
win32api.keybd_event(win32con.VK_SHIFT, 0, win32con.KEYEVENTF_KEYUP, 0)
print("✅ 文本已输入")
def press_enter():
"""按回车"""
print("\n⏎ 按回车发送...")
time.sleep(0.3)
win32api.keybd_event(win32con.VK_RETURN, 0, 0, 0)
time.sleep(0.05)
win32api.keybd_event(win32con.VK_RETURN, 0, win32con.KEYEVENTF_KEYUP, 0)
print("✅ 已按回车")
def auto_input_to_kiro(x=1228, y=720, text="继续", wait_time=0):
"""
自动化输入到 Kiro
参数:
x: X 坐标
y: Y 坐标
text: 要输入的文本
wait_time: 开始前等待时间(秒)
"""
print("=" * 60)
print("Kiro 自动化输入")
print("=" * 60)
print(f"\n📍 目标位置: ({x}, {y})")
print(f"📝 输入文本: {text}")
if wait_time > 0:
print(f"\n⏰ {wait_time} 秒后开始...")
time.sleep(wait_time)
print()
# 步骤1: 查找并激活 Kiro 窗口
print("[1/4] 查找并激活 Kiro 窗口")
print("-" * 60)
hwnd, title = find_and_activate_kiro()
if not hwnd:
return False
# 步骤2: 移动鼠标并点击
print("\n[2/4] 移动鼠标并点击目标位置")
print("-" * 60)
move_and_click(x, y)
# 步骤3: 输入文本
print("\n[3/4] 输入文本")
print("-" * 60)
input_text(text)
# 步骤4: 按回车
print("\n[4/4] 按回车发送")
print("-" * 60)
press_enter()
print("\n" + "=" * 60)
print("✨ 完成!")
print("=" * 60)
return True
if __name__ == "__main__":
import sys
# 默认参数
x = 1228
y = 720
text = "继续"
wait_time = 3
# 解析命令行参数
if len(sys.argv) > 1:
# 检查是否有 --now 参数(立即执行)
if "--now" in sys.argv:
wait_time = 0
sys.argv.remove("--now")
# 解析位置和文本
if len(sys.argv) > 1:
if sys.argv[1].isdigit():
# 格式: python kiro_auto_input.py 1228 720 继续
x = int(sys.argv[1])
if len(sys.argv) > 2 and sys.argv[2].isdigit():
y = int(sys.argv[2])
text = " ".join(sys.argv[3:]) if len(sys.argv) > 3 else "继续"
else:
text = " ".join(sys.argv[2:]) if len(sys.argv) > 2 else "继续"
else:
# 格式: python kiro_auto_input.py 继续
text = " ".join(sys.argv[1:])
print("\n" + "=" * 60)
print("配置信息")
print("=" * 60)
print(f"目标位置: ({x}, {y})")
print(f"输入文本: {text}")
print(f"等待时间: {wait_time} 秒")
print("=" * 60)
if wait_time > 0:
print(f"\n⏰ {wait_time} 秒后开始...")
print("💡 提示:脚本会自动激活 Kiro 窗口\n")
time.sleep(wait_time)
auto_input_to_kiro(x, y, text, wait_time=0)