-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_by_handle.py
More file actions
208 lines (161 loc) · 6.31 KB
/
input_by_handle.py
File metadata and controls
208 lines (161 loc) · 6.31 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
"""
直接通过句柄向文本框输入文本
"""
import win32gui
import win32con
import win32api
import time
def input_to_handle(hwnd, text):
"""
直接向指定句柄的控件输入文本
参数:
hwnd: 控件句柄(整数)
text: 要输入的文本
"""
print(f"🎯 目标句柄: {hwnd}")
print(f"📝 输入文本: {text}")
try:
# 获取控件信息
class_name = win32gui.GetClassName(hwnd)
print(f" 控件类名: {class_name}")
# 检查控件是否存在
if not win32gui.IsWindow(hwnd):
print("❌ 句柄无效或窗口已关闭")
return False
# 获取父窗口
parent_hwnd = win32gui.GetParent(hwnd)
if parent_hwnd:
parent_title = win32gui.GetWindowText(parent_hwnd)
print(f" 父窗口: {parent_title}")
# 激活父窗口
win32gui.SetForegroundWindow(parent_hwnd)
time.sleep(0.3)
# 方法1:使用 WM_SETTEXT
print("\n💡 方法1: WM_SETTEXT")
win32api.SendMessage(hwnd, win32con.WM_SETTEXT, 0, text)
time.sleep(0.2)
# 验证是否成功
length = win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0)
if length > 0:
print(f"✅ 成功!文本长度: {length}")
else:
print("⚠️ 方法1失败,尝试方法2...")
# 方法2:逐字符发送 WM_CHAR
print("\n💡 方法2: WM_CHAR")
for char in text:
win32api.SendMessage(hwnd, win32con.WM_CHAR, ord(char), 0)
time.sleep(0.01)
print("✅ 已发送字符")
# 发送回车键
print("\n⏎ 发送回车...")
win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
time.sleep(0.05)
win32api.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
print("✅ 已按回车")
return True
except Exception as e:
print(f"❌ 错误: {e}")
return False
def input_to_kiro_chat(text="继续"):
"""
向 Kiro 聊天框输入文本(自动查找)
"""
print("🔍 查找 Kiro 窗口...")
# 查找 Kiro 窗口
def find_kiro(hwnd, results):
if win32gui.IsWindowVisible(hwnd):
title = win32gui.GetWindowText(hwnd)
if "Kiro" in title:
results.append((hwnd, title))
return True
results = []
win32gui.EnumWindows(find_kiro, results)
if not results:
print("❌ 未找到 Kiro 窗口")
return False
main_hwnd, title = results[0]
print(f"✅ 找到: {title}")
print(f" 主窗口句柄: {main_hwnd}")
# 查找所有子控件
print("\n🔍 查找文本输入框...")
def find_controls(child_hwnd, results):
class_name = win32gui.GetClassName(child_hwnd)
control_text = win32gui.GetWindowText(child_hwnd)
# 查找可能的输入框
if win32gui.IsWindowVisible(child_hwnd) and win32gui.IsWindowEnabled(child_hwnd):
# 常见的输入框类名
if any(keyword in class_name for keyword in ['Edit', 'Text', 'Input', 'Chrome']):
results.append((child_hwnd, class_name, control_text))
return True
controls = []
win32gui.EnumChildWindows(main_hwnd, find_controls, controls)
print(f"✅ 找到 {len(controls)} 个可能的输入控件:")
for i, (hwnd, class_name, text) in enumerate(controls):
print(f" [{i}] 句柄: {hwnd:10d} 类名: {class_name:30s} 文本: {text[:50]}")
if not controls:
print("\n❌ 未找到输入控件")
print("💡 尝试使用键盘模拟...")
# 激活窗口并模拟键盘输入
win32gui.SetForegroundWindow(main_hwnd)
time.sleep(0.5)
# 使用剪贴板粘贴
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("✅ 已粘贴文本")
# 回车
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("✅ 已按回车")
return True
# 尝试向每个控件输入
print(f"\n📝 尝试输入文本: {text}")
for i, (hwnd, class_name, _) in enumerate(controls):
print(f"\n尝试控件 [{i}]...")
if input_to_handle(hwnd, text): # 使用参数 text,不是控件文本
return True
print("\n❌ 所有控件都失败")
return False
if __name__ == "__main__":
import sys
print("=" * 60)
print("向 Kiro 输入文本")
print("=" * 60)
if len(sys.argv) > 1:
if sys.argv[1].isdigit():
# 直接使用句柄
hwnd = int(sys.argv[1])
text = " ".join(sys.argv[2:]) if len(sys.argv) > 2 else "继续"
print(f"\n📌 使用指定句柄: {hwnd}")
print(f"📝 输入文本: {text}")
print("\n⏰ 3 秒后开始...")
time.sleep(3)
input_to_handle(hwnd, text)
else:
# 使用文本作为输入
text = " ".join(sys.argv[1:])
print(f"\n📝 输入文本: {text}")
print("\n⏰ 3 秒后开始...")
time.sleep(3)
input_to_kiro_chat(text)
else:
# 默认输入"继续"
text = "继续"
print(f"\n📝 输入文本: {text}")
print("\n⏰ 3 秒后开始...")
time.sleep(3)
input_to_kiro_chat(text)
print("\n" + "=" * 60)
print("完成!")
print("=" * 60)