首页 > 解决方案 > Pyautogui 键盘命令不适用于菜单栏分配

问题描述

我正在编写一个脚本来自动化 SAP GUI 中的某些管理任务。我可以单击导航、使用选项卡、输入字符串并在表单中按 Enter。

问题:当我使用 pyautogui(例如: pyautogui.press('F12') )发送菜单键分配时,它们似乎不起作用。这迫使我不得不使用其他替代方法(错误的鼠标点击等等)。知道为什么这些不起作用吗?

我可以在没有的情况下工作 - 但我想知道是否有人了解到底发生了什么......如果可以的话,那就太好了!

标签: pythonpyautogui

解决方案


pyautogui.press 区分大小写,或者至少在 Windows 上。所以你需要说

pyautogui.press('f12')

这是一个键列表

定义这个存根可能很方便

show_trivial_nags = True #maybe be able to toggle this on the command line

def ci_press(x):
    if x != x.lower() and show_trivial_nags:
        print("WARNING: press commands should be in lower case.")
    pyautogui.press(x.lower())

我使用的测试用例是在 alt-tab 中使用 Firefox,

import pyautogui
pyautogui.hotkey('alt', 'tab')
# Comment out the first, and the search bar still appears. Comment out the second, and it doesn't.
pyautogui.press('F3')
pyautogui.press('f3')
exit()

推荐阅读