rpa_sendKeys
模拟键盘输入,输入的按键或单个按键动作(即按住并释放)自动激活到活动窗口
注意:该函数运行时,键盘操作可能影响结果,若机器人中使用了该API,机器人执行时避免人为操作键盘
1. 函数
rpa_sendKeys(keys)
- keys:字符串,输入的按键描述,可用的字符串包括:
{SCROLLLOCK}, {VK_SPACE}, {VK_LSHIFT}, {VK_PAUSE}, {VK_MODECHANGE},
{BACK}, {VK_HOME}, {F23}, {F22}, {F21}, {F20}, {VK_HANGEUL}, {VK_KANJI},
{VK_RIGHT}, {BS}, {HOME}, {VK_F4}, {VK_ACCEPT}, {VK_F18}, {VK_SNAPSHOT},
{VK_PA1}, {VK_NONAME}, {VK_LCONTROL}, {ZOOM}, {VK_ATTN}, {VK_F10}, {VK_F22},
{VK_F23}, {VK_F20}, {VK_F21}, {VK_SCROLL}, {TAB}, {VK_F11}, {VK_END},
{LEFT}, {VK_UP}, {NUMLOCK}, {VK_APPS}, {PGUP}, {VK_F8}, {VK_CONTROL},
{VK_LEFT}, {PRTSC}, {VK_NUMPAD4}, {CAPSLOCK}, {VK_CONVERT}, {VK_PROCESSKEY},
{ENTER}, {VK_SEPARATOR}, {VK_RWIN}, {VK_LMENU}, {VK_NEXT}, {F1}, {F2},
{F3}, {F4}, {F5}, {F6}, {F7}, {F8}, {F9}, {VK_ADD}, {VK_RCONTROL},
{VK_RETURN}, {BREAK}, {VK_NUMPAD9}, {VK_NUMPAD8}, {RWIN}, {VK_KANA},
{PGDN}, {VK_NUMPAD3}, {DEL}, {VK_NUMPAD1}, {VK_NUMPAD0}, {VK_NUMPAD7},
{VK_NUMPAD6}, {VK_NUMPAD5}, {DELETE}, {VK_PRIOR}, {VK_SUBTRACT}, {HELP},
{VK_PRINT}, {VK_BACK}, {CAP}, {VK_RBUTTON}, {VK_RSHIFT}, {VK_LWIN}, {DOWN},
{VK_HELP}, {VK_NONCONVERT}, {BACKSPACE}, {VK_SELECT}, {VK_TAB}, {VK_HANJA},
{VK_NUMPAD2}, {INSERT}, {VK_F9}, {VK_DECIMAL}, {VK_FINAL}, {VK_EXSEL},
{RMENU}, {VK_F3}, {VK_F2}, {VK_F1}, {VK_F7}, {VK_F6}, {VK_F5}, {VK_CRSEL},
{VK_SHIFT}, {VK_EREOF}, {VK_CANCEL}, {VK_DELETE}, {VK_HANGUL}, {VK_MBUTTON},
{VK_NUMLOCK}, {VK_CLEAR}, {END}, {VK_MENU}, {SPACE}, {BKSP}, {VK_INSERT},
{F18}, {F19}, {ESC}, {VK_MULTIPLY}, {F12}, {F13}, {F10}, {F11}, {F16},
{F17}, {F14}, {F15}, {F24}, {RIGHT}, {VK_F24}, {VK_CAPITAL}, {VK_LBUTTON},
{VK_OEM_CLEAR}, {VK_ESCAPE}, {UP}, {VK_DIVIDE}, {INS}, {VK_JUNJA},
{VK_F19}, {VK_EXECUTE}, {VK_PLAY}, {VK_RMENU}, {VK_F13}, {VK_F12}, {LWIN},
{VK_DOWN}, {VK_F17}, {VK_F16}, {VK_F15}, {VK_F14}
~ is a shorter alias for {ENTER}
修饰符:
'+': {VK_SHIFT}
Shift键'^': {VK_CONTROL}
Control键'%': {VK_MENU}
Alt键
如何使用修饰符:
rpa_sendKeys('^a^c') # 全选(Ctrl + A)并复制到剪贴板(Ctrl + C)
rpa_sendKeys('+{INS}') # 从剪贴板插入(Shift + Ins)
rpa_sendKeys('%{F4}') # 使用Alt + F4关闭活动窗口
可以为特殊键指定重复计数。输入两次按Enter键。{ENTER 2}
如何按住键盘上的键或释放键:
rpa_sendKeys("{VK_SHIFT down}
"jdarobot"
"{VK_SHIFT up}") # 输入JDAROBOT
rpa_sendKeys("{h down}"
"{e down}"
"{h up}"
"{e up}"
"llo") # 输入hello
使用花括号来转义修饰符并将保留的符号键入为单个键:
rpa_sendKeys("{^}"
"a"
"{^}"
"c") # 类型字符串“^a^c”(不会按下Ctrl)
2. 返回值
该函数没有返回值。
3. 示例
- 通过在京麦顶部输入框中输入“jm://settings”打开系统设置
# 变量定义
jmPath = rpa_view_readParam('京麦目录')
jmPath = jmPath + '/JMWorkStation.exe'
print(jmPath)
# 连接京麦
appId = rpa_uia_connectApp(ppath=jmPath)
if not appId:
rpa_showMsgBox('连接京麦失败,错误码:%d' % rpa_getLastErrorCode())
rpa_exit()
# 顶部输入框中打开系统设置
rpa_mouse_click(appId, '顶部输入框', left=True, index=0, waitElement=True)
# 输入jm://settings,采用rpa_sendHotKey
# for key in 'jm://settings':
# rpa_sendHotKey(key, ctrl=False, alt=False, shift=False, win=False)
# 输入jm://settings,采用rpa_sendKeys
rpa_sendKeys('jm://settings')
# 回车
rpa_sendKeys('{ENTER}')
# 等待元素,判断是否打开成功
if rpa_uia_waitElement(appId, "页面-系统设置标题", timeout=10.0, index=0):
print('打开成功')
else:
print('打开失败')
4. 中文输入
中文的输入与系统的输入法相关,使用该函数无法直接输入。但结合剪贴板和快捷键,可以轻松实现:
# 写入剪贴板
text = '使用JDA,打造个人专属机器人'
rpa_writeClipboard(text)
# 发送Ctrl + V按键粘贴
rpa_sendKeys('^v')
rpa_delay(0.5)