输入与键盘

输入与键盘

keys()enter()input(ctrl=…) 里的 ctrl 都是 UIA 控件对象,需先用 find()wait_ctrl() 拿到,再传给后续方法:

# ① 先获取控件(下面所有 ctrl=ctrl 都指这个变量)
ctrl = auto.find(ctrl_type='EditControl', aid='chat_input')

# 若控件可能尚未出现,可轮询等待:
# ctrl = auto.wait_ctrl(ctrl_type='EditControl', aid='chat_input', timeout=5.0)

# ② 输入文字
auto.input('你好', ctrl=ctrl, paste=True)              # 默认 paste=True,走剪贴板
auto.input('你好', ctrl=ctrl, paste=False)             # uia 模式可尝试 ctrl.Input
auto.input('你好', ctrl_type='EditControl', aid='chat_input')  # 或不先 find,直接传属性

# 快捷:点击 + 输入 + 回车
auto.send_msg('你好', aid='chat_input')
auto.clear_input(ctrl=ctrl)                            # 内部 hotkey Ctrl+A + Delete

# ③ 按键
# 组合键 → hotkey(post/auto 常用,需 hwnd)
auto.hotkey(0x11, 0x41)                  # Ctrl+A
auto.hotkey(0x11, 0x43)                  # Ctrl+C

# 复杂键序列 → keys + ctrl(UIA SendKeys,mode='uia' 或 auto 有 ctrl 时)
auto.keys('{Ctrl}a{Delete}', ctrl)       # 全选后删除(需 ctrl,post 模式不可用)
auto.keys('{Enter}', ctrl)               # 单键也可传 ctrl

# 单功能键 → keys 或 enter(post 模式仅支持 _VK_MAP 中的单键)
auto.enter(ctrl)                         # 等同 keys('{Enter}', ctrl)
auto.keys('{PageDown}', ctrl)

键盘两种方式对比:

方法写法示例ctrl组合键
hotkey(vk1, vk2, …)auto.hotkey(0x11, 0x41)不需要post/auto 推荐
keys(str, ctrl)auto.keys('{Ctrl}a', ctrl)组合键建议必传uia/auto + ctrl
enter(ctrl)auto.enter(ctrl)可选单键 Enter

keys()post/mouse 下仅支持下列单键字面量(与 _VK_MAP 一致):{Enter} {Esc} {Tab} {PageUp} {PageDown} {Home} {End} {Up} {Down} {Left} {Right} {Delete} {BackSpace} {Space} {Insert} {F1}{F8}

常用 VK 码速查

修饰键:

VKVK
Ctrl0x11Shift0x10
Alt0x12Win0x5B

功能键(hotkey 组合用 VK;keys 在 post 模式仅支持下列单键字面量):

VKVK
Enter0x0DEsc0x1B
Tab0x09Space0x20
Backspace0x08Delete0x2E
Insert0x2DHome0x24
End0x23PageUp0x21
PageDown0x22↑↓←→0x26/0x28/0x25/0x27
F1–F80x700x77

字母与数字(hotkey 用 VK;规律:A–Z = 0x410x5A,0–9 = 0x300x39):

VKVKVKVK
A0x41C0x43V0x56X0x58
S0x53Z0x5AF0x460–90x300x39

常见组合示例:

auto.hotkey(0x11, 0x41)   # Ctrl+A  全选
auto.hotkey(0x11, 0x43)   # Ctrl+C  复制
auto.hotkey(0x11, 0x56)   # Ctrl+V  粘贴
auto.hotkey(0x11, 0x58)   # Ctrl+X  剪切
auto.hotkey(0x11, 0x5A)   # Ctrl+Z  撤销
auto.hotkey(0x11, 0x53)   # Ctrl+S  保存
auto.hotkey(0x11, 0x0D)   # Ctrl+Enter
  • 组合键优先 hotkey(0x11, 0x43) 等形式
  • 复杂 SendKeys 字符串(含 {Ctrl})用 keys('…', ctrl),且需 ctrl 来自步骤 ①
  • paste() 内部调用 keys('{Ctrl}v', ctrl),同样需要控件或 uia 模式

API 参考

输入与键盘

✨input

向目标控件输入文字;会先 click(ctrl) 聚焦。

auto.input('你好', ctrl=editor, mode='uia')
auto.input('abc', ctrl=editor, mode='uia', paste=False)

参数

参数名类型默认值描述
textstr要输入的字符串
ctrlControlNone目标控件
ctrl_typestr''未传 ctrl 时用于查找
namestr''未传 ctrl 时用于查找
aidstr''未传 ctrl 时用于查找
clsstr''未传 ctrl 时用于查找
depthint10搜索深度
pasteboolTrueTrue:剪贴板粘贴;False:UIA 尝试 ctrl.Input();Post 用 WM_CHAR
modestrNone操作模式

返回值

  • 类型:WinAuto

✨keys

SendKeys 语法按键:'{Enter}''{Ctrl}a''{PageDown}' 等。

auto.keys('{Ctrl}a{Delete}', ctrl)
auto.keys('{PageDown}', ctrl)

参数

参数名类型默认值描述
keysstrSendKeys 字符串
ctrlControlNoneUIA 控件;uia 模式必须
modestrNone操作模式

返回值

  • 类型:WinAuto

说明

模式要求
uia必须ctrl
post组合键走 _post_combo(需 ctrl 的 NativeWindowHandle);单键走 _VK_MAP
mouse物理按键

不支持时抛 RuntimeError

✨hotkey

虚拟键码组合键,如 Ctrl+A。

auto.hotkey(0x11, 0x41)

参数

参数名类型默认值描述
*vk_codesint虚拟键码,如 0x11, 0x41(Ctrl+A)
modestrNone操作模式
ctrlControlNonePost 模式定位编辑框句柄

返回值

  • 类型:WinAuto

说明

UIA 模式不支持,请用 keys('{Ctrl}a', ctrl)

✨enter

等同 keys('{Enter}', ctrl, mode=mode)

auto.enter(ctrl)

参数

参数名类型默认值描述
ctrlControlNoneUIA 控件
modestrNone操作模式

返回值

  • 类型:WinAuto

✨send_msg

clickinputenter 一步发送消息。

auto.send_msg('你好', aid='chat_input')

参数

参数名类型默认值描述
textstr要发送的文字
ctrlControlNone输入框控件
ctrl_typestr''未传 ctrl 时用于查找
namestr''未传 ctrl 时用于查找
aidstr''未传 ctrl 时用于查找
clsstr''未传 ctrl 时用于查找
depthint10搜索深度
pasteboolTrue是否走剪贴板粘贴
modestrNone操作模式

返回值

  • 类型:WinAuto

✨clear_input

清空输入框:UIA 可 ValuePattern.SetValue('');否则全选+删除。

auto.clear_input(ctrl=ctrl)

参数

参数名类型默认值描述
ctrlControlNone输入框控件
modestrNone操作模式

返回值

  • 类型:WinAuto

✨type_and_send

clear_inputinputenter 一步完成(清空后输入并发送)。

ctrl = auto.find(ctrl_type='EditControl', aid='chat_input', depth=12)
auto.type_and_send('你好,测试消息', ctrl, mode='uia')

参数

参数名类型默认值描述
textstr要发送的文字
ctrlControlNone输入框控件
modestrNone操作模式
pasteboolTrue输入是否走剪贴板

返回值

  • 类型:WinAuto

✨select_all

全选输入框内容。

auto.select_all(ctrl=ctrl, mode='uia')

参数

参数名类型默认值描述
ctrlControlNone输入框控件
modestrNone操作模式

返回值

  • 类型:WinAuto

说明

模式路径
uiaTextPattern 全选 或 SendKeys {Ctrl}a
postEM_SETSEL / PostMessage Ctrl+A(仅标准 Edit 或支持 WM_KEY 的控件)
其它keys('{Ctrl}a', ...)

上一页查找与点击下一页剪贴板